2012-02-16 42 views
1

此代碼正在傳入一個函數,並且沒有繼承我的EmployeesController對象的狀態。我能做些什麼來將我的EmployessController對象綁定到焦點事件上?CoffeeScript類字段未定義

class @EmployeesController 
    constructor: (@dateInput) -> 
    @dateInput.focus(@searchInputGainedFocus) 


    searchInputGainedFocus: -> 
    console.debug @dateInput 

換句話說,當我給dateInput焦點時,console.debug打印undefined。

回答

5

使用"fat arrow" (=>)結合searchInputGainedFocus對象:

箭頭=>可以用於兩者的脂肪定義一個函數,並且將其綁定到的this的電流值,右當場。

class @EmployeesController 
    constructor: (@dateInput) -> 
    @dateInput.focus(@searchInputGainedFocus) 

    searchInputGainedFocus: => 
    console.debug @dateInput 
:使用基於回調的庫時,像原型或jQuery的,[...]

所以定義searchInputGainedFocus這樣,這是很有幫助