Created
October 17, 2013 08:56
-
-
Save bguiz/7021513 to your computer and use it in GitHub Desktop.
This file contains 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
var Foo = Backbone.Model.extend({ | |
/* some code here */ | |
}); | |
//Default sollection | |
Foo.Collection = Backbone.Collection.extend({ | |
model: Foo | |
/* some code here */ | |
}); | |
//# alt 1 - upfront, best when updates don't occur very often, and access occurs very often | |
Foo.Collection = Backbone.Collection.extend({ | |
model: Foo, | |
initialize: function(initialModels, options) { | |
this.listenTo(this, 'add remove sync', this._regroup); | |
this._groupedData = []; | |
}, | |
_regroup: function() { | |
//TODO parse collection and group by bar | |
}, | |
getGroupedModels: function() { | |
return this._groupedData; | |
} | |
}); | |
//# alt 2 - lazy, best when updates occur very often, and access occurs not as often | |
Foo.Collection = Backbone.Collection.extend({ | |
model: Foo, | |
initialize: function(initialModels, options) { | |
this._groupedData = []; | |
}, | |
_regroup: function() { | |
//TODO parse collection and group by bar | |
}, | |
getGroupedModels: function() { | |
this._regroup(); | |
return this._groupedData; | |
} | |
}); | |
//# alt 3 - optimised, uses a flag to track whether updates have occurred | |
Foo.Collection = Backbone.Collection.extend({ | |
model: Foo, | |
initialize: function(initialModels, options) { | |
this.listenTo(this, 'add remove sync', this._flagForRegroup); | |
this._groupedData = []; | |
this._needsRegroup = true; | |
}, | |
_flagForRegroup: function() { | |
this._needsRegroup = true; | |
}, | |
_regroup: function() { | |
this._needsRegroup = false; | |
//TODO parse collection and group by bar | |
}, | |
getGroupedModels: function() { | |
if (this._needsRegroup) { | |
this._regroup(); | |
} | |
return this._groupedData(); | |
} | |
}); | |
//# alt 4 - use a separate collection. useful when data needs to be a backbone collection instead of POJSO array | |
Foo.GroupedCollection = Backbone.Collection.extend({ | |
initialize(models, options) { | |
this._origCollection = options.origCollection; | |
this.listenTo(this._origCollection, 'add remove sync', this._regroup); | |
}, | |
_regroup: function() { | |
//parse this._origCollection and group by bar | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment