2011-11-24 32 views
0

$(this)不支持cofeescript和backbone.Its生成有效的代碼,但我不明白爲什麼它不起作用。對於解決我使用$(event.target)。

Jobmatch.Views.Jobs ||= {} 

class Jobmatch.Views.Jobs.JobView extends Backbone.View 
template: JST["recommendation/templates/jobs/job"] 

initialize:() -> 
    @ajs = new Jobmatch.Collections.ApplicantJobsCollection() 
    @ajs.reset(@options.applicant_jobs || []) 
    @aj = new @ajs.model() 
    @index = @options.index || 0 

events: 
    "click .job_apply" : "apply" 

tagName: "tr" 

apply: (event)-> 
    target = $(this) // As this is not working as I expected,So I used below line. 
    target = $(event.target) 
    if @options.user_jobmatch_valid 
    @ajs.create({job_id: @model.get('id') }) 
    target.parents("a.job_apply").prev().click(); 
    else 
    target.parents("a.job_apply").next().click(); 
    false 


render: -> 
    $(this.el).html(@template(@model.toJSON())) 
@ 

這cofeescript產生了下面的代碼:

JobView.prototype.apply = function(event) { 
    var target; target = $(this); // not working it is not elementt object 
     target = $(event.target);// this is element object ,working fine  
     target.parents("a.job_apply").prev().click(); 
}; 
+3

你真的應該發佈一些示例代碼。如果不在上下文中看到該代碼,就不可能知道上面這句中的「this」是指什麼。 –

+0

我在打擊回答中添加了代碼, – Neelesh

+0

可以請您粘貼整個視圖嗎?我對初始化特別感興趣:function(){} ... – Sander

回答

1

這一切取決於上下文,

this是綁定到您的視圖本身某些情況下, 拿這個例子:

var myView = Backbone.View.extend({ 
    events: { "click a" : "myfunction" }, 
    initialize: function(){ 
     _.bindAll(this, 'render', 'myfunction'); 
    }, 
    render: function(){ 
     // rendering data here... 
    }, 
    myfunction: function(e){ 
     console.log(e.target); // will log the clicked DOM element => <a> 
     console.log(this); // will log the view => myView 
    } 

}); 

window.v = new myView({ el : $('#mydiv') }); 
window.v.render(); 

正如你所見,如果你應該運行此示例,您可以看到thise.target之間的差異。但是,這全部歸功於initialize方法中的keyline。

_.bindAll(this, 'methodname', [ methodnames ]); 

此結合中的給定方法1時的視圖this。 如果您從該列表中刪除myfunction,則console.log(this);會記錄該元素本身。 但是,您將無法從視圖中獲取數據或功能.... 如果您將視圖綁定到this,則可以自由選擇。

+0

因此,這意味着我不能用 「這個」 下面: **事件: 「點擊.job_apply」: 「應用」 申請:(事件) - > 目標= $(本)//目標= $ (event.target)** 我不能在這裏訪問這個 – Neelesh

+0

這總是可用的,但是如果你使用'_bindAll()'''''這個''this''指向被點擊的元素,像我這樣做,你將無法做到這一點,但你可以使用'$(event.target)'而不是...積極的方式來使用它,就是你可以設置或從你的視圖中獲取東西,因爲'this'指的是視圖對象(正如我在文章中所解釋的)...意味着你可以執行'this.model.set({name:$(event.target).val()});'=>你是實際上在'this'和html元素中使用視圖'$(event.target)' – Sander

+0

準確地說,我理解你的解釋,但擔心其生成的代碼,在我上面的代碼中,這是生成有效的jquery code.for上面的代碼它生成以下代碼: '$('job_aaply')。click(function ){ alert($(this))//它應該是jquery對象,但沒有運氣 })' 所以我不知道爲什麼這是行不通的。儘管感謝您的幫助。 – Neelesh

1

當事件通過Backbone視圖綁定時,this將被設置爲視圖本身。這是正常的jQuery不同的地方this指DOM元素

因此,在一個骨幹視圖:

events: { "click a" : "myfunction" }, 
... 
function myfunction(e) { 
    this // refers to the view 
    e.target // refers to the element 
} 

而在正常的jQuery:

$('a').on('click', myfunction); 
... 
function myfunction() { 
    this // refers to the element 
    e.target // also refers to the element 
} 

在這兩種情況下,你可以使用e.target指代元素

+0

這是__very__正確的答案,你(問題的作者)應該將此標記爲已接受,因爲所有其他人都包含非常不可接受的錯誤。 –

2

在主幹文檔中,http://backbonejs.org/#View-delegateEvents 它聲明清楚:

所有附回調綁定到視圖越區切換 將jQuery之前,所以被調用回調時,這仍然參閱 視圖對象。

這解決了許多面向對象程序員使用jQuery時遇到的問題之一,即它覆蓋了所有事件處理程序中的上下文。如前所述,您可以經常使用事件,該事件作爲處理程序的參數傳遞。但是,event.target通常可以引用子元素,而不是this,它總是指使用查詢選擇的元素。例如:

<a href="#"><b>my link</b></a> 

$('a').on('click', myfunction); function myfunction(event) { 
    this // refers to the "a" element 
    event.target // refers to the "b" element 
} 

在這種情況下,與骨幹網連接鼠標事件時,元素與兒童,使用event.currentTarget去該事件處理程序被連接到元素的引用。

events: { 
    "click a" : "myFunction" }, 
... 
function myFunction(event) { 
    var $this = $(event.currentTarget); 
    // Now, $this is the same as jQuery's $(this) 
}