在多个视图中触发事件
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际的 UI 事件是在测验视图中完成的。您正确地删除了元素并销毁了模型。现在您有两个选择:
The actual UI event is done within the quiz view. You correctly remove the element and destroy the model. Now you have two choices: