admin管理员组

文章数量:1289911

What exactly happens when you save a Backbone model? Here's the best I can piece together by reading the documentation here:

  1. model.save([attributes], [options]) is called
  2. A "change" event is fired (but only if the attributes are new)
  3. The server is notified of the change?
  4. 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:

  1. model.save([attributes], [options]) is called
  2. A "change" event is fired (but only if the attributes are new)
  3. The server is notified of the change?
  4. 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 badges
Add a ment  | 

2 Answers 2

Reset to default 8

The 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:

  1. the attributes passed to the function are set, a change event is fired if the values changed
  2. save delegates the request to model.sync or Backbone.sync
  3. sync serializes the data to a JSON string by calling JSON.stringify(model.toJSON())
  4. 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 (or collection.url/id)
  5. When the request pletes, the model is updated with the server response, if any, and triggers a change event accordingly.
  6. 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