Question

window.PR= Backbone.View.extend({
el: $(".ptMain"),
initialize:function (model) {
},
events:{
     "click .yes":"removeThis"
},
removeThis:function(){
    console.log("test");
    console.log(this)
},
render:function () {
    var _this = this;
    $(this.el).html(this.template({remove:this.model.toJSON()}));
    $(".yes").on("click",this.removeThis,_this)
    return this;
}})

I have button with class "yes". I cant bind event in "events" because this element in other view. When i bind without context its ok, but when i add context this to .on i have error

TypeError: handleObj.handler.apply is not a function .apply( matched.elem, args ); jquery-1.9.1.js on line 3074 Please, help to fix this problem.

Was it helpful?

Solution

I personally use the $.proxy() function for these cases. It allows you to call a function with a specific context. You would end up with this as render function.

render:function () {
    var _this = this;
    $(this.el).html(this.template({remove:this.model.toJSON()}));
    $(".yes").on("click", $.proxy(this.removeThis, _this));
    return this;
}})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top