2014-12-02 53 views
3

林在運行時的方法試圖在運行時生成的餘燼和代碼一些方法我想是產生emberJS

App.TestController = Ember.ArrayController.extend App.AnotherMixin, 

    unsubmitted: Em.computed.filterBy("model", "unsubmitted", true) 
    submitted: Em.computed.filterBy("model", "submitted", true) 
    canceled: Em.computed.filterBy("model", "canceled", true) 
# Rather than using above methods I'm trying to generate them with meta-programming. 

    that: @ 
    defineAttributes: (-> 
    [ 
     "unsubmitted" 
     "submitted" 
     "cancelled" 
    ].forEach (f) -> 
     Em.defineProperty that , f, Em.computed.filterBy("model", f, true) 
     return 
    return 
).on("init") 

但它不是產生方法。那麼我有什麼遺漏?

回答

3

您正在將that定義爲控制器上的屬性,但試圖在您的defineAttributes方法中將其用作局部變量。將that更改爲方法中的局部變量,它應該可以正常工作。或者更好,只需使用Coffeescript的胖箭頭功能來保持當前值this

defineAttributes: (-> 
    ['unsubmitted', 'submitted', 'cancelled'].forEach (f) => 
     Em.defineProperty this, f, Em.computed.filterBy('model', f, true) 
).on('init')