繁体   English   中英

骨干收集未触发添加事件

[英]Backbone Collection Not firing Add Event

所以我有一个问题,即我有一个骨干集合,该集合用于创建将数据保存到REST API的功能。 数据被保存到服务器,模型被添加到当前集合,但是集合的add事件不会被触发。下面是代码片段:视图初始化函数

intialize : function() {
        this.listenTo(this.collection, 'add', this.updateList);
    },      

updateList函数仅执行控制台日志。使用该集合保存数据的views函数为:

cards = this.collection;
        debugger
        cards.create(data, {
            success : function(model, response) {
                console.log("success on saving card");
                console.log(response);
                console.log("Updating list");
                console.log(cards);
            },
            error : function(model, response) {
                console.log("error on saving card");
                console.log(model);
                console.log("response");
                console.log(response);
            }
        })
        return false;

在您的视图中尝试以下代码:

initialize: function() {
    this.collection.on('add', this.updateList, this);
}

要么:

var someCollection = new SomeCollection();
var view = new SomeView({collection: someCollection});
view.listenTo(someCollection, 'add', view.updateList);

您为什么不只使用: this.model.on('change', doAction, this); 在视图内? 如果我理解正确,这是一个更好的解决方案,因为您的模型正在更改。 另外,我在任何地方都找不到在On()函数中强制使用ListenTo()的地方,您能告诉我您在哪里看到的吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM