admin管理员组文章数量:1289911
What exactly happens when you save a Backbone model? Here's the best I can piece together by reading the documentation here:
model.save([attributes], [options])
is called- A "change" event is fired (but only if the attributes are new)
- The server is notified of the change?
- A "sync" event is called once the server returns
But I'm a Backbone noob and I'm sure someone else could do a way better job of explaining.
I'm partly just curious what happens. I'm also having trouble understanding how Backbone es up with the JSON object it sends to the server. I'm having a separate problem where the JSON object is not what I want it to be, but I don't know how to change it.
What exactly happens when you save a Backbone model? Here's the best I can piece together by reading the documentation here:
model.save([attributes], [options])
is called- A "change" event is fired (but only if the attributes are new)
- The server is notified of the change?
- A "sync" event is called once the server returns
But I'm a Backbone noob and I'm sure someone else could do a way better job of explaining.
I'm partly just curious what happens. I'm also having trouble understanding how Backbone es up with the JSON object it sends to the server. I'm having a separate problem where the JSON object is not what I want it to be, but I don't know how to change it.
Share Improve this question edited May 23, 2017 at 12:09 CommunityBot 11 silver badge asked Jul 10, 2012 at 14:47 Jason SwettJason Swett 45.2k70 gold badges230 silver badges359 bronze badges2 Answers
Reset to default 8The detailed process can be found in the annotated source code for Backbone.Model.save and Backbone.sync.
If you ignore options.wait
and options.silent
, your deposition is mostly correct.
When you issue a model.save
:
- the attributes passed to the function are set, a change event is fired if the values changed
save
delegates the request tomodel.sync
orBackbone.sync
sync
serializes the data to a JSON string by callingJSON.stringify(model.toJSON())
- An Ajax request is sent to sent to server, a POST request for a new object, a PUT for an update. The target URL is defined by
model.url
(orcollection.url/id
) - When the request pletes, the model is updated with the server response, if any, and triggers a
change
event accordingly. - Success or error callbacks are called, a
sync
event is triggered if no success callback is defined.
Usually, you can customize this behaviour by overriding model.toJSON
or model.sync
first,I suggest you read the source code of the backbone, is really very simple.Default backbone and server-side interaction is achieved through backbone.sync.
second,You can trace debug model.save method of code again, naturally know the details. I suggest you start here: http://backbonejs/examples/todos/index.html
本文标签: javascriptWhat exactly happens when you save a Backbone modelStack Overflow
版权声明:本文标题:javascript - What exactly happens when you save a Backbone model? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741452284a2379554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论