开发者

BackboneJS, create and destroy, but no update method?

I noticed that in BacbkoneJS there is a method in the Collection object that allows you to add a new model to that collection, while at the same time pushing the new model to the Server. This is convenient because it will only add the new model to the collection if the ajax request is successful. It will also update the ID on the model with the one returned from the server.

There is also a similar method on the Model object called destroy, this will only destroy the model if the ajax request returns 200.

How开发者_Go百科 can I achieve something similar to this with Update, where when I set data on my model, it will attempt to save the model to the server, ensure a 200 status, and then fire the "change" event?

The problem is If I set the model data (I have to do this in order to call save()), then the change event fires.

Thanks


The Backbone.Model.save() method allows you to specify the properties you want to change. Looking at the annotated source code, it looks like the "model.set()" happens on a successful response from the server.

So, instead of doing

myModel.set({this:"that"});
myModel.save();

Do

myModel.save({this:"that"});

and the change event should fire after a successful save.

Note: I have not tested this - this is just from reading the source.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜