Backbone events are not working

For some reason I don't know why my event in a Backbone View doesn't work. I tried to Google for some answer but I didn't find anything that would help me.

Basically, my code is this:

Backbone:

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>

Your example works fine for me in this fiddle.

As explunit noted, though, your el should reference an element and should not be a jQuery object. $el takes care of that. According to 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.

Check that you're correctly loading the Jquery, Underscore and Backbone scripts (in that order). Also make sure you're script is being executed once the page is ready and not, say, before your DOM has finished loading (causing your view to not attach to anything).

链接地址: http://www.djcxy.com/p/70278.html

上一篇: 在浏览器中更改URL而不使用JavaScript加载新页面

下一篇: 骨干事件不起作用