【问题标题】:Backbone events are not working骨干事件不起作用
【发布时间】:2013-03-22 16:52:09
【问题描述】:

由于某种原因,我不知道为什么我在主干视图中的事件不起作用。 我试图用谷歌搜索一些答案,但我没有找到任何对我有帮助的东西。

基本上,我的代码是这样的:

主干:

var ViniView = Backbone.View.extend({
    el: $('.container'),
    events: {
        "click .clickme" : "render"
    },
    render: function() {
        alert("please, work");
    }
});
    
new ViniView;

HTML

<div class="container">
    <button class="clickme">
    test
    </button>
</div>

【问题讨论】:

  • 你能举一个jsfiddle.net的例子吗?
  • 你不需要 el 来引用一个特定的项目,而不是一个类吗?

标签: backbone.js


【解决方案1】:

您的示例在this fiddle 中对我很有效。

正如 explunit 所指出的,您的 el 应该引用一个元素,而不应该是一个 jQuery 对象。 $el 负责这一点。根据the docs:

All views have a DOM element at all times (the el property), whether they've already been inserted into the page or not.

检查您是否正确加载了 Jquery、Underscore 和 Backbone 脚本(按此顺序)。还要确保在页面准备就绪后执行脚本,而不是在 DOM 完成加载之前执行(导致您的视图不附加到任何内容)。

【讨论】:

  • 嘿伙计,谢谢!我犯的错误是从我准备好的函数中执行我的主干脚本。现在它的工作,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多