成功するとイベントfetchがトリガー"change"されます:
フェッチ model.fetch([options])
[...]"change"サーバーの状態が現在の属性と異なる場合、イベントがトリガーされます。
したがって、fetchが何かを行う場合、"change"リッスンできる があります。
myApp.on("initialize:before", function () {
this.currentUser = new UserModel();
this.currentUser.on('change', function() {
myApp.vent.trigger('currentUser');
});
this.currentUser.fetch();
});
"currentUser"が他の方法で変更された場合にもイベントがトリガーthis.currentUserされ、それが必要な場合とそうでない場合があります。関数を 1 回だけ呼び出したい場合は、現在のsuccessハンドラーを使用するのがおそらく最も簡単です。onハンドラーが呼び出されたときに、ハンドラーを引き続き使用およびアンバインドできます。
myApp.on("initialize:before", function () {
var triggerCurrentUser = _.bind(function() {
myApp.vent.trigger('currentUser');
this.currentUser.off('change', triggerCurrentUser);
}, this);
this.currentUser = new UserModel();
this.currentUser.on('change', triggerCurrentUser);
this.currentUser.fetch();
});
を使用することもできます_.onceが、それではノーオペレーション コールバック関数が起動してしまうので、その必要はありません。