2013-06-25 135 views
0

我有这个集合的集合和表视图。骨干模型设置值不会触发更改事件

App.Views.TableContainer = Backbone.View.extend({ 
    el: '#some-table, 

    clearTable: function(){ 
     this.collection.each(function(person){ 
      person.set('deleteMe', 1); 
     }); 
    }  
}); 

在表中每一行的视图:

App.Views.PersonRow = App.Views.BaseViewTemplate.extend({ 
    template: template("person-row-template"), 
    tagName: 'tr', 

    events: { 
     'click' : 'removeFromTable' 
    }, 

    initialize: function(){ 
     this.model.on('change', this.removeRow(), this); 
    }, 

    removeRow: function(){ 
     console.log('removing'); 
     console.log(this.model.toJSON()); 
     if(this.model.get('deleteMe') == 1){ 
      this.$el.remove(); 
      App.publisherTableContainer.removeFromContainer(this.model); 
     } 

    }, 

    removeFromTable: function(e){ 
     var me = this; 
     this.$el.fadeOut(300); 
     App.personTableContainer.removeFromContainer(this.model); 

    } 
}); 

执行clearTable TableContainer时,代码PersonRow的功能removeRow没有执行(可悲)

任何想法?

谢谢! 罗伊

+0

'console.log'的输出是什么? –

+0

'Backbone.View.extend({el:'#some-table,',有一个报价'''丢失,错字? –

+0

它是一个错字... – royB

回答

3

错误是:(这是一种常见的一个可能就是你的改变功能无法正常工作)

this.model.on('change', this.removeRow(), this); 

应该是:

this.model.on('change', this.removeRow, this); 

上功能第二个参数应该是一个回调函数。现在你正在发送removeRow返回的任何内容。这将工作,如果该功能实际上是返回一个回调函数,但我想这不是在这种情况下:)

+0

第一个肯定只是一个错字,不需要但他的问题肯定来自第二个错误,解释为什么它应该没有括号,但可能不清楚OP。 – Loamhoof

+0

我已经编辑了答案。谢谢Loamhoof – twibakish

相关问题