Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone events are not working

Tags:

backbone.js

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>
like image 762
J.Vinicius Avatar asked Mar 22 '13 16:03

J.Vinicius


People also ask

How does Backbone js work?

Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

What is Backbone in programming?

Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).

Why use Backbone js?

BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.


1 Answers

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

like image 80
Cianan Sims Avatar answered Oct 04 '22 17:10

Cianan Sims