문제

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.

도움이 되었습니까?

해결책

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;
}})
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top