I've got this filter for breaking up an array into smaller arrays based on 
the "size" argument. Here's a jsbin of the non-working 
version: http://jsbin.com/UmOMAgA/80/edit
Check out some previous revisions (a lot of people have toyed around with 
it) for the working version using a cache.

app.filter('partition', function() {
  return function(arr, size) {
    var newArr = [];
    
    for (var i=0; i<arr.length; i+=size) {
        newArr.push(arr.slice(i, i+size));        
    }
    
    return newArr;
  }; 
});

This causes an infinite digest. I think that's because of the new nested 
arrays. The only solution I've found is to do some awkward caching with 
JSON strings:

app.filter('partition', function($cacheFactory) {
  var arrayCache = $cacheFactory('partition');
  var filter = function(arr, size) {
    if (!arr) { return; }
    var newArr = [];
    for (var i=0; i<arr.length; i+=size) {
        newArr.push(arr.slice(i, i+size));        
    }
    var cachedParts;
    var arrString = JSON.stringify(arr);
    cachedParts = arrayCache.get(arrString+size); 
    if (JSON.stringify(cachedParts) === JSON.stringify(newArr)) {
      return cachedParts;
    }
    arrayCache.put(arrString+size, newArr);
    return newArr;
  };
  return filter;
});

There's got to be a better way, right!?

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to