2014-12-02 62 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')