Backbone:删除不会绑定?

发布于 2024-12-25 07:45:55 字数 1015 浏览 0 评论 0原文

我正在尝试绑定我的主干视图之一,以便在删除模型时,它也会从不同的视图中删除。

看起来很简单,但我似乎无法将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安人多梦 2025-01-01 07:45:55

解决了。如果其他人遇到这个问题,问题出在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:

  • Ensure the collection is passed through to the view
  • 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

断爱 2025-01-01 07:45:55

这是怎么回事:

MyView = Backbone.View.extend({
    initialize: function(){
        this.model.bind('destroy', this.remove(), this)
    }

})

Whats about this:

MyView = Backbone.View.extend({
    initialize: function(){
        this.model.bind('destroy', this.remove(), this)
    }

})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文