Skip to content

@derickbailey /1.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
onReset callbacks with Backbone collections
personById: function(id){
var person = new Person();
person.id = id;
person.fetch({
success: function(model, response){
App.showPerson(model);
}
})
}
personById: function(id){
var persons = new PersonCollection();
// note that "bind" is now "on" in Backbone v0.9.x
persons.on("reset", function(collection, response){
var person = collection.get(id);
App.showPerson(person);
});
persons.fetch();
}
// App.js
// ------
// some initialization code that happens elsewhere, using Backbone.Marionette
App.addInitializer(function(){
App.persons = new PersonCollection();
App.persons.fetch();
});
// PersonRouter.js
// ---------------
// The router callback that needs to get the person
personById: function(id){
App.persons.onReset(function(collection){
var person = collection.get(id);
App.showPerson(person);
});
}
OnResetCollection = Backbone.Collection.extend({
constructor: function(){
var args = slice(arguments);
Backbone.Collection.prototype.constructor.apply(this, args);
this.onResetCallbacks = [];
this.on("reset", this.collectionReset, this);
},
onReset: function(callback){
this.onResetCallbacks.push(callback);
this.collectionLoaded && this.fireResetCallbacks();
},
collectionReset: function(){
if (!this.collectionLoaded) {
this.collectionLoaded = true
}
this.fireResetCallbacks();
},
fireResetCallbacks: function(){
var callback = this.onResetCallbacks.pop();
if (callback){
callback(this);
this.fireResetCallbacks();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.