I’m trying to run very simple view in backbone.js
Here is the code:
(function($){
window.templateLoaderView = Backbone.View.extend({
events: {
'click #add_contact': 'loadTaskPopup'
},
initialize: function () {
alert('templateLoaderView - initialize');
_.bindAll(this, 'render');
},
render: function() {
alert('templateLoaderView - render');
},
loadTaskPopup: function() {
alert('templateLoaderView - loadTaskPopup');
}
});
})(jQuery);
$(document).ready(function() {
window.templateLoaderView = new templateLoaderView();
});
<div id="add_contact">CLICK HERE</div>
When page loads, it alerts this alert('templateLoaderView - initialize');, but when I click the div, nothing happens.
Could you please tell what I do wrong?
There are a couple of things that are going wrong.
this.elas a DIV but it isn’t rooted in anything#add_contactrenderbecause nothing is ever callingrender.The most simple way to get your click handler to work is to tell the view which element to attach to:
Going further…
Though, you may want your DIV to be created inside your view… it would go a little something like this: