2014-01-15 15 views
1
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; 
}}) 

我有按鈕,帶班「是」。我不能綁定「事件」中的事件,因爲這個元素在其他視圖中。當我綁定沒有上下文其確定,但是當我添加上下文這一點。當我有錯誤骨幹。對上下文錯誤

TypeError:handleObj.handler.apply不是函數 .apply(matched.elem,args); jquery-1.9.1.js在線3074 請幫忙解決這個問題。

回答

1

我個人使用$.proxy()函數爲這些情況。它允許你使用特定的上下文來調用一個函數。你最終會將其作爲渲染函數。

render:function() { 
    var _this = this; 
    $(this.el).html(this.template({remove:this.model.toJSON()})); 
    $(".yes").on("click", $.proxy(this.removeThis, _this)); 
    return this; 
}}) 
+0

THX,它'真的幫我,但如果某人能解釋如何使用。對背景下,西港島線是偉大 – Anton

+0

與骨幹實施。對功能只能在骨幹分子使用。所以,做你想做的,你會在一個骨幹視圖把$(「是」)的內容。 $(「。yes」)是一個jQuery元素,因此使用了.on()jQuery。我做了一個小提琴,希望能夠澄清這一點。 http://jsfiddle.net/6PP8p/ – donnywals

+0

謝謝你。你是怎麼做到的? – urmurmur