11

バックボーン モデルを保存すると、具体的にはどうなりますか? こちらのドキュメントを読んで、私がまとめることができる最高のものを次に示します。

  1. model.save([attributes], [options])と呼ばれる
  2. 「変更」イベントが発生します (ただし、属性が新しい場合のみ)
  3. サーバーに変更が通知されますか?
  4. サーバーが戻ると、「同期」イベントが呼び出されます

しかし、私はバックボーンの初心者であり、他の誰かが説明のより良い仕事をすることができると確信しています.

私は部分的に何が起こるのか興味があります。また、Backbone がサーバーに送信する JSON オブジェクトをどのように作成するのか理解できません。JSONオブジェクトが私が望むものではないという別の問題がありますが、それを変更する方法がわかりません。

4

2 に答える 2

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 decomposition 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 completes, 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

于 2012-07-10T15:18:55.507 に答える
-1

まず、バックボーンのソースコードを読むことをお勧めします。これは非常に単純です。デフォルトのバックボーンとサーバー側の相互作用は、backbone.syncを介して実現されます。

次に、デバッグmodel.saveメソッドのコードを再度トレースして、詳細を自然に知ることができます。ここから始めることをお勧めします:http: //backbonejs.org/examples/todos/index.html

于 2012-07-10T15:20:14.213 に答える