2013-07-17 18 views
1

我有以下控制器:Ember.js控制器方法返回的功能文字

Whistlr.OrganizationController = Ember.ObjectController.extend 
    location: (-> 
    location = this.city+", "+this.country 
    return location 
) 

這在我的模板:

{{location}} 

但是,而不是渲染一個字符串,如「新紐約,美國」燼呈現:

function() { var location; location = this.city + ", " + this.country; return location; } 

我在做什麼錯在這裏?

回答

2

你忘了把它定義爲一個Computed Property

Whistlr.OrganizationController = Ember.ObjectController.extend 
    location: (-> 
    location = this.get('city') + ", " + this.get('country') 
    return location 
).property('city', 'country') 

不要忘記,當您使用屬性的值使用get()。換句話說,使用this.get('foo')而不是this.foo。此外,由於您使用CoffeeScript,因此您的代碼寫得更好:

Whistlr.OrganizationController = Ember.ObjectController.extend 
    location: (-> 
    @get('city') + ", " + @get('country') 
).property('city', 'country') 
+0

感謝您的詳細解答。這正是我所需要的。 – nullnullnull