2

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

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

updateList 函数只做一个控制台日志。使用集合保存数据的视图函数是:

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;
4

2 回答 2

1

在您的视图中尝试此代码:

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);
于 2013-09-27T20:45:24.163 回答
0

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

于 2013-09-28T19:00:42.130 回答