For now I don't see how this could be solved in a filter (the only
approach that would probably work would be to keep references to the
filtered arrays so we are not systematically re-creating new objects
but this might not be good memory-wise). It would be great to have
some insight from the AngularJS core team.
Hi PawelThanks for your solution! Meanwhile I found my own working solution, in which I simply set $$hashKey of a chunk to the index of that chunk. That works just as well, though it has a potential problem, which I believe your solution has as well: ng-repeat will consider two chunks the same even if they contain different values. In my solution this happens all the time, since the first chunk always gets $$hashKey 0, etc. In your solution, it happens when the string representation of different chunks are incidentally the same - or for the chunks [12, 3, 4] and [1, 23, 4], for example. However, this doesn't really matter in practice, since the output of the "chunk" filter will typically be used for a later ng-repeat, and the differences will be caught there.I guess your implementation is the most elegant. I'll insert mine at the end of this post. I've refactored out some functions to make it more readable, which of course makes it more verbose.Both solutions have the major problem that they are bound to the internal workings of Angular... but I guess it's the best we can do.Thank you so much for your time, Pawel. Everybody now has a working "chunk" filter, so that using Twitter Bootstrap with Angular is not a dark art anymore. I hope the Angular developers will create an official, clean "chunk" filter sometime in the future.myApp.filter('chunk', function() {function chunkArray(array, chunkSize) {var result = [];var currentChunk = [];for (var i=0; i<array.length; i++) {currentChunk.push(array[i]);if (currentChunk.length == chunkSize) {result.push(currentChunk);currentChunk = [];}}if (currentChunk.length > 0) {result.push(currentChunk);}return result;}function defineHashKeys(array) {for (var i=0; i<array.length; i++) {array[i].$$hashKey = i;}}return function(array, chunkSize) {if (!(array instanceof Array)) return array;if (!chunkSize) return array;var result = chunkArray(array, chunkSize);defineHashKeys(result);return result;}});
group = (array, num) -># return grouped data_.memoize group, (array, num) ->return array if not angular.isArray(array)
hash = ""hash += item.$$hashKey for item in arrayhash
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/IEIQok-YkpU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.
if you want return new collection, you just need to save javascript links.
I wrote factory for this case:
app.factory('linker', function () { var links = {} return function (arr, key) { var link = links[key] || [] arr.forEach(function (newItem, index) { var oldItem = link[index] if (!angular.equals(oldItem, newItem)) link[index] = newItem }) link.length = arr.length return links[key] = link } })
you can see here how it works: http://plnkr.co/edit/2Uc5zsFgVnK3ltHOUUQx?p=preview