【发布时间】:2014-08-28 09:43:54
【问题描述】:
我有一个模型,其中包含集合。在我的网络应用程序中,我有一个项目模型,其中我有一个名为“collaborators”的属性,该属性实际上是用户的集合。
在我的视图的初始化中,我有以下内容,
this.model.get('collaborators').on( 'change', alert("!!"), this);
当视图首次加载时,我会收到一个警报,在视图中我有一个事件,它会触发以下事件,
var newModel = new app.User({ id: this.clickedElement.data('userId') });
console.log(this.model.get(this.formSection)); // shows that the collection has 4 models
newModel.fetch({
success: function() {
that.model.get(that.formSection).add(newModel);
console.log(that.model.get(that.formSection)); //shows that the collection has 5 models
}
});
正如您在上面的代码中看到的那样,我正在记录我的合作者集合,它显示长度为 4,在成功方法中获取之后,我是否将刚刚获取的模型添加到该集合中,然后记录再次收集,这次返回长度为 5。
这对我来说意味着添加成功,那么“添加”的事件监听器在初始页面查看后没有触发吗?
【问题讨论】:
-
我没有看到
add听众。只有change- 但这是在您的合作者收藏中。集合不会触发change事件,我不确定它是否会冒泡到父模型。 -
即使我这样做 `this.model.get('collaborators').on('add', alert("!!"), this);'它仍然只在视图的初始加载时触发,而不是在我向集合中添加更多数据时触发。
-
您是否尝试过仅在父模型上使用
change事件? -
是的
this.listenTo('this.model', 'change', alert("!!"));完全一样
标签: javascript backbone.js backbone-views backbone.js-collections