Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  140] [ 4]  / answers: 1 / hits: 19724  / 13 Years ago, mon, january 16, 2012, 12:00:00

If I have done my homework correctly, I have come to learn that Backbone does not have built-in save event that is triggered when a model is saved using the model's save method (even though there is a destroy event).



I have also learned that Backbone has a nifty mechanism for creating custom events using the Backbone.Events object. Using the latter works, but I have the impression that it is not fine-grained enough for my needs.



My setup is as follows. I have a table (view) built up of rows (views) with each row having a reference to a model. When the model is saved, I'd like to update/render the row to reflect the changes.



How does one go about creating a save event that is triggered when a model is saved so that the table row (view) that has a reference to that model is updated?



In other words, I'd like to be able to do the following:



this.model.bind('save', this.render);

More From » events

 Answers
68

Just 3 days ago, a commit was made to Backbone that triggers a sync event when the model is successfully saved. This commit hasn't been release yet, though, so you will need to download the source code from the github account if you want to use it.



View = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'onModelSaved');
this.model.bind('sync', onSuccessCallback);
},

onModelSaved: function(model, response, options) {
//perform your after save logic
}
});

[#87990] Saturday, January 14, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harleyaleenag

Total Points: 678
Total Questions: 121
Total Answers: 105

Location: Papua New Guinea
Member since Thu, Jul 9, 2020
4 Years ago
harleyaleenag questions
Thu, May 5, 22, 00:00, 2 Years ago
Wed, Aug 19, 20, 00:00, 4 Years ago
;