【问题标题】:can't destroy view of related model不能破坏相关模型的视图
【发布时间】:2013-02-07 16:15:29
【问题描述】:

我有一个模型正在表单视图中被销毁,我还需要它从列表视图中删除相关元素。

在我的表单视图中

删除:函数(){ this.model.destroy(); this.el.remove(); }

在我的列表视图中我有

初始化:函数(){ this.model.on('remove',this.delete); }, 删除:函数(){ 警报(“删除”); this.el.remove(); this.el.unbind(); }

当我删除项目时,会触发 listView 中的删除,但我收到错误 cannot call method remove of undefined

我也试过$(this.el).remove(),但没有运气。 listView 是从集合中添加到另一个视图中的

itemCollection.each(this.add); 添加:函数(){ var create = new Myapp.Views.Items({model:item}); $(this.el).append(create.el); }

【问题讨论】:

    标签: backbone.js model-binding destroy


    【解决方案1】:
    1. this.el替换为this.$el

    2. 不要使用 delete 作为方法名

    3. this.model.on('remove',this.delete); 更改为this.model.on('remove',_.bind(this.delete, this));

    4. jsbin

    【讨论】:

    • 这行得通,但似乎过于复杂。 'on' 事件不应该与模型的删除绑定吗?它之前触发过,我为什么需要你写的所有额外代码。你能澄清一下吗?
    猜你喜欢
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多