Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I add preventDefault() backbone.js

How do I add preventDefault() to the <button> in the event? What I have below is not working .. thanks

var View = Backbone.View.extend({

    initialize: function () {
        //console.log('initializing ' + this.options.blankOption);
        this.template = $('#list-template').children();
    },
    el: '#container',
    events: {
        'click button' : 'render'
    },
    render: function(){

        this.events.preventDefault(); // not working ????

        var data = this.model.get('data');

        $.each(data,function (i,v) {
            console.log(data.text + " " + data.href);
        });

    }
});
like image 747
Hello-World Avatar asked Dec 02 '25 02:12

Hello-World


2 Answers

this.events is not a event object. so it not work

try this:

render: function(event){
    event && event.preventDefault();

    var data = this.model.get('data');

    $.each(data,function (i,v) {
        console.log(data.text + " " + data.href);
    });
}
like image 171
anhulife Avatar answered Dec 04 '25 16:12

anhulife


Receive the event object as an argument

render: function(evt){

    evt.preventDefault();

    var data = this.model.get('data');

    $.each(data,function (i,v) {
        console.log(data.text + " " + data.href);
    });

}
like image 24
Aditya Manohar Avatar answered Dec 04 '25 16:12

Aditya Manohar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!