Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active November 7, 2019 21:10
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katowulf/bee266e31aa60cb0eed6 to your computer and use it in GitHub Desktop.
Save katowulf/bee266e31aa60cb0eed6 to your computer and use it in GitHub Desktop.
Filter records loaded into AngularFire based on some criteria.
// this will be much more efficient than $watch()
app.factory('FilteredArray', function($firebaseArray) {
function FilteredArray(ref, filterFn) {
this.filterFn = filterFn;
return $firebaseArray.call(this, ref);
}
FilteredArray.prototype.$$added = function(snap) {
var rec = $firebaseArray.prototype.$$added.call(this, snap);
if( !this.filterFn || this.filterFn(rec) ) {
return rec;
}
};
return $firebaseArray.$extend(FilteredArray);
});
app.controller('...', function($scope, FilteredArray) {
var ref = new Firebase('https://kato-books.firebaseio.com/');
$scope.data = FilteredArray(ref, function(rec) {
return rec.author !== 'Stephen King';
})
});
@emil-alexandrescu
Copy link

@rainabba You should initialize array with new FilteredArray(...).

@douwevdijk
Copy link

I know it is are a couple of years later :). but how can this be used in combination with updates / removals?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment