J.Vinicius
J.Vinicius

Reputation: 148

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>

Upvotes: 7

Views: 4830

Answers (1)

Cianan Sims
Cianan Sims

Reputation: 3429

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).

Upvotes: 4

Related Questions