H. Adolf H. Adolf - 1 month ago 15
jQuery Question

change Jquery events to Backbone.js events

I have small backbone.js test application I am trying to learn backbone.js. Now I have some jquery onclick actions, but i want to change that actions to backbone. Sorry for bad English. I read backbone documentation but don't get how to achieve what I want. this is my view.render function

$('#item').on('click', function(event){
do some stuff here
});
$('#item').on('click', function(event){
do some stuff here
});


Thanks in advance
Regards Adolf

Answer

You need to use backbone.js view events property i will show you how to do that.

var myView = Backbone.View.extend({
 initialize: function(){
 },
 events: {
    'click #item': 'itemClicked',
    'click .anyclass': 'elementClicked'
 },
 itemClicked: function(event){
     event.preventDefault();
     /*do some stuff here*/
 },
 elementClicked: function(event){
     event.preventDefault();
    /* do some other stuff here*/
 },
 render:function(){
    /*some code here*/
 }
});

And please read this article so next time you will know how to properly use backbone.js view events https://www.sitepoint.com/backbone-basics-events/

Comments