在多个视图中触发事件

发布于 2024-10-19 02:54:53 字数 830 浏览 1 评论 0原文

我正在使用 Backbone.js 构建一个应用程序,它现在有两个视图:一个 IndexView 和一个 QuizPartial。 IndexView 呈现页面的大部分(一些图表等),并且它包含许多 QuizPartials。我的问题是,当用户单击其中一个部分中的“删除”链接时,应删除该部分并销毁相应的模型,而 IndexView 则呈现一个按钮来创建新的测验。但是,我无法让 IndexView 响应该事件。

代码:

class QuizPartial extends Backbone.View
  tagName: "div"
  className: "quiz"
  events:
    "click a.delete": "delete_quiz" # Works fine

  initialize: -> @render()

  delete_quiz: ->
    if confirm "Are you sure you want to delete this test?"
      $(@el).remove()
      @model.destroy()
    false

然后是索引视图:

class IndexView extends Backbone.View
  tagName: "div"
  id: "quizzes_index"
  events:
    "click .quiz a.delete": "render_new_quiz_button" # Never fires

  initialize: -> @render()

  # etc...

我应该做些什么不同的事情吗?

谢谢!

I'm putting together an app using Backbone.js, which has two Views right now, an IndexView and a QuizPartial. The IndexView renders the bulk of the page (some graphs and whatnot), and it contains many QuizPartials. My issue is that when a user clicks a 'delete' link in one of the partials, the partial should be deleted and the appropriate model destroyed, while the IndexView renders a button to create a new quiz. However, I can't get the IndexView to respond to that event.

Code:

class QuizPartial extends Backbone.View
  tagName: "div"
  className: "quiz"
  events:
    "click a.delete": "delete_quiz" # Works fine

  initialize: -> @render()

  delete_quiz: ->
    if confirm "Are you sure you want to delete this test?"
      $(@el).remove()
      @model.destroy()
    false

And then the index view:

class IndexView extends Backbone.View
  tagName: "div"
  id: "quizzes_index"
  events:
    "click .quiz a.delete": "render_new_quiz_button" # Never fires

  initialize: -> @render()

  # etc...

Is there something I should be doing differently?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

も星光 2024-10-26 02:54:53

实际的 UI 事件是在测验视图中完成的。您正确地删除了元素并销毁了模型。现在您有两个选择:

  • 让您的 IndexView 侦听测验集合上的“删除”事件。
  • 从测验视图触发新事件以通知正在收听的人

The actual UI event is done within the quiz view. You correctly remove the element and destroy the model. Now you have two choices:

  • Have your IndexView listen for the "remove" event on your Quiz collection.
  • Trigger a new event from your quiz view to notify whoever is listening
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文