2013-09-27 43 views
2

所以我有一個問題,我有一個骨幹集合,正在使用創建函數來保存數據到REST API。該數據被保存到服務器和模型添加到當前集合,但隨後進行採集附加事件不是fired.Below是代碼 意見初始化函數的片段只有骨幹集合沒有發射添加事件

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

的updateList功能的確,使用收集的數據保存控制檯log.The觀點功能是:

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; 

回答

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); 
+0

我試過這個,但它沒有工作,所以我不知道最新的問題。我使用backbone 1.0,它建議使用listenTo()而不是 –

+0

如果你在骨幹文檔中看到,它建議使用''listenTo()''而不是''on()''?這個文檔說:「使用這種形式而不是on()的好處是,listenTo()允許對象跟蹤事件,並且可以在以後一次刪除它們 - 這是不同的你在之前的評論中寫的是什麼。 –

+0

嘗試添加''console.log('event is fired);''給你的回調函數...請查看我更新的代碼。 –

0

你爲什麼不使用:this.model.on('change', doAction, this);裏面的視圖?如果我理解正確,那麼從模型改變開始,這是一個更好的解決方案。 加上,我無法在任何地方找到ListenTo()在On()函數上是強制性的地方,你能告訴我你在哪看到它嗎?