私はこれに困惑しています。単純なバックボーン リレーショナル モデルがあります。
window.Site = Backbone.RelationalModel.extend({
idAttribute: "_id",
// These are the relations to the user model.
relations: [{
type: Backbone.HasMany,
key: 'users',
relatedModel: 'window.User'
}],
});
そして、私のユーザーモデル(サイトモデルに関連しています)は次のようになります:
//Site User model
// -------------
window.User = Backbone.RelationalModel.extend({
});
まだプロトタイピング中なので、ユーザー モデルは意図的に単純化されています。
サイトとユーザーをハイドレートするためにサーバーから受け取る JSON は、次のようになります。
{
_id: foo,
users: [
username: bar,
password: fizz
]}
私が困惑しているのはリスナーです。SiteView (私の SiteCollection をレンダリングする) にあるイベントは次のようになります。
initialize: function() {
//basic bindings
this.model.bind('change', this.setSave, this);
this.model.bind('destroy', this.remove, this);
// bindings to sub-models
this.model.bind('add:users', this.setDetailsView, this);
this.model.bind('remove:users', this.setSave, this);
this.model.bind('change:users', this.setSave, this);
add:users
andイベントは正常に発生しremove:users
ますが、change:users
イベントは発生しません。Users モデルをレンダリングする DetailsView には、いくつかの単純なイベント バインディングもあります。
initialize: function() {
this.model.bind('change', this.render, this);
this.model.bind('destroy', this.remove, this);
しかし、何らかの理由でchange:users
、SiteView のイベントは発生しませんがchange
、DetailsView のイベントは発生します。
これは次の理由による可能性があります。
- Users モデルの
change
イベントは、2 つの異なるビューで 2 回バインドされていますか? - ユーザー モデルは双方向ではありませんか?