2013-07-17 20 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