【发布时间】:2012-01-06 01:54:44
【问题描述】:
我正在尝试绑定我的一个主干视图,以便在删除模型时,它也会从另一个视图中删除。
看起来很简单,但我似乎无法让 destroy 方法绑定到视图。我可以绑定到更改或新模型,但删除不会触发。我记得模糊地读过一些关于绑定删除的怪癖,但我不记得它是什么,更重要的是,我不记得如何绕过它。
任何想法表示赞赏。简而言之,如果有人可以提供一个将删除调用绑定到以下相关代码的示例:
源代码视图
class BackboneDemo.Views.Tasks.ShowView
# ...
events:
"click #mark_task_completed" : "markAsCompleted"
"click #delete_task" : "destroy"
destroy: () ->
$('#contentArea').html('')
$('#contentWrapper').css('display', 'none')
@model.destroy()
this.remove()
return false
编辑:仍然没有快乐。我在下面添加了更多代码以准确显示问题所在
型号
class MyModel extends Backbone.Model
# ...
destroy: () ->
console.log 'this is getting hit'
super
目标视图
class TargetView extends Backbone.View
# ...
initialize:() ->
_.bindAll(@, 'destroy', 'testmethod', 'render')
@model.bind('destroy', @testmethod)
testmethod: () ->
console.log 'but this is not getting hit'
【问题讨论】:
-
绑定到“删除”或“销毁”或“删除”的部分在哪里?
-
它在目标视图代码中,在初始化中。我尝试了各种不同的线路,希望让它工作,通常沿着 @model.bind('remove', @test_function) 的线路。
标签: javascript backbone.js coffeescript