1

我有以下控制器:

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

这在我的模板中:

{{location}}

但是,ember 不会渲染诸如“New York, USA”之类的字符串,而是渲染:

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

我在这里做错了什么?

4

1 回答 1

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')
于 2013-07-17T19:28:52.560 回答