Backbone:删除不会绑定?
我正在尝试绑定我的主干视图之一,以便在删除模型时,它也会从不同的视图中删除。
看起来很简单,但我似乎无法将 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'
I'm trying to bind one of my backbone views so that, when a model is deleted, it's removed from a different view as well.
Seems pretty straightforward, but I can't seem to get the destroy method to bind to the view. I can bind to changes or new models, but deletes just aren't triggering. I remember vaguely reading something about an eccentricity with binding to delete, but I can't remember what it was or, more importantly, how to get around it.
Any ideas appreciated. In short, if anyone could offer an example of binding a delete call to Relevant code below:
Source View
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
Edit: Still no joy. I've added more code below to show precisely where the issue is
Model
class MyModel extends Backbone.Model
# ...
destroy: () ->
console.log 'this is getting hit'
super
Target View
class TargetView extends Backbone.View
# ...
initialize:() ->
_.bindAll(@, 'destroy', 'testmethod', 'render')
@model.bind('destroy', @testmethod)
testmethod: () ->
console.log 'but this is not getting hit'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决了。如果其他人遇到这个问题,问题出在backbone-rails gem 以及它的 destroy() 函数如何工作。有一个待处理的拉取请求可以解决该问题,但其缺点是:
将默认的 destroy() 代码替换为:
销毁:() ->
getViewAndCollection = ((view) -> return -> {collection: @options.collection, view: @})(@)
@model.destroy()
成功:(模型,响应)->
vars = getViewAndCollection()
vars.collection.remove模型
vars.view.remove()
错误:(模型,响应)->
# 无论您想要什么错误功能
您都可以在此处
Solved. In case anyone else comes across this issue, the problem is with the backbone-rails gem and how it's destroy() function works. There's a pending pull request that solves the issue, but the short of it is:
Replace the default destroy() code with this:
destroy: () ->
getViewAndCollection = ((view) -> return -> {collection: @options.collection, view: @})(@)
@model.destroy()
success: (model, response) ->
vars = getViewAndCollection()
vars.collection.remove model
vars.view.remove()
error: (model, response) ->
# Whatever you want for error functionality
You can see more at the pending pull request here
这是怎么回事:
Whats about this: