Skip to content

Instantly share code, notes, and snippets.

@saadtazi
Last active December 12, 2015 10:38
Show Gist options
  • Save saadtazi/4760030 to your computer and use it in GitHub Desktop.
Save saadtazi/4760030 to your computer and use it in GitHub Desktop.
can.Model.List.prototype.filter = function(cb) {
var res = [];
debugger;
this.each(function(amodel, pos, models) {
var ares = cb.call(amodel, pos, models);
if (ares === true) {res.push(amodel)}
});
return res
}
// usage
var MyModel = can.Model({});
var myInst = new MyModel.List([{attr1: 1, att2: 2}, {attr1: 'one', att2: 'two'}]);
t = myInst.filter(function(pos, list) {
// this = an item in list - should return true (not just a non-falsy value) if you want to keep that item
if (pos % 2 == 0) {return true;}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment