Created
August 7, 2012 18:58
-
-
Save fguillen/3288350 to your computer and use it in GitHub Desktop.
Backbone AutoUpdatedCollection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
In the scenario you have a _shared Collection_ which contains every Model of one class, | |
and this Collection is used to populate _sub-Collections_ those have to be _updated_ | |
when the shared Collection is updated. | |
*/ | |
App.AutoUpdatedCollection = Backbone.Model.extend({ | |
initialize: function( opts ){ | |
this.name = opts.name; | |
this.sourceCollection = opts.sourceCollection; | |
this.filterField = opts.filterField; | |
this.filterValue = opts.filterValue; | |
this.filteredCollection = new Backbone.Collection; | |
this.filteredCollection.reset( this.filterCollection() ); | |
this.sourceCollection.on( "change:" + this.filterField, this.filterModel, this ); | |
this.sourceCollection.on( "remove", this.removeModel, this ); | |
this.sourceCollection.on( "add", this.addModel, this ); | |
}, | |
filterCollection: function(){ | |
console.log( "AutoUpdatedCollection.filterCollection", this.name ); | |
// var _self = this; | |
var result = | |
this.sourceCollection.filter( function( model ) { | |
return model.get( this.filterField ) == this.filterValue; | |
}, this); | |
console.log( "AutoUpdatedCollection.filterCollection.result", this.name, result.length, result ); | |
return result; | |
}, | |
filterModel: function( model, val ){ | |
console.log( "AutoUpdatedCollection.filterModel", this.name, model.id, val ); | |
if( val == this.filterValue ){ | |
this.addModel( model ); | |
} else { | |
this.removeModel( model ); | |
} | |
}, | |
removeModel: function( model ){ | |
if( this.filteredCollection.get( model.id ) ){ | |
console.log( "AutoUpdatedCollection.removeModel", this.name, model.id ); | |
this.filteredCollection.remove( model ); | |
} | |
}, | |
addModel: function( model ){ | |
if( model.get( this.filterField ) == this.filterValue ){ | |
console.log( "AutoUpdatedCollection.addModel", this.name, model.id, Math.random() ); | |
this.filteredCollection.add( model ); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment