You can use sync
like described in another post already made or a number of other events.
Refer to Collection.create() and Collection.fetch() for which events you can listenTo
.
initialize: function() {
this.listenTo(this.model, "sync", this.render);
},
createFeed: function(e){
e.preventDefault();
var feed_url = $('#new_feed_name').val();
this.model.create({
url: feed_url
});
}
You can listen for the add
event in the Router to trigger the second post. The view for the articles can listen for some event triggered by the force_update
post to render, much like the initialize function above.
home: function() {
var publications = new SimpleGoogleReader.Collections.Publications();
var articles = new SimpleGoogleReader.Collections.Articles();
var pubIndex = new SimpleGoogleReader.Views.PublicationsIndex({model: publications});
var artIndex = new SimpleGoogleReader.Views.ArticlesIndex({model: articles});
// create a method to replace the force_update post in your publications view
articles.listenTo(publications, "add", articles.force_update);
publications.fetch();
articles.fetch();
}
By listening to sync
, you can remove the fetch({success:fn})
callback.
Update
The main idea is to leverage Backbone's event system to talk between the collections and the views. If the standard use cases apply to you, use Backbone's defaults otherwise make use of trigger("custom-event")
.
Inside the declaration of SimpleGoogleReader.Collections.Articles
// collection "add" event callbacks pass the model as the first n
// param to the callback
force_update: function(publication) {
var data = {
feed_url: publication.get("url")
};
var callback = _.bind(function() {
this.trigger('force-update');
}, this);
$.post('/articles/force_update', data).done(callback);
}
});
Inside the SimpleGoogleReader.Views.ArticlesIndex
initialize: function() {
this.listenTo(this.model, "force-update", this.render);
}