Coffeescript中的余烬计算属性

Coffeescript中的余烬计算属性,coffeescript,ember.js,Coffeescript,Ember.js,我想在Coffeescript中实现以下Javascript代码 App.ItemView = Ember.View.extend({ classNameBindings: ['itemId'], itemId: function() { console.log(this.get('content')); return "item-%@".fmt(this.get('content.id')); }.property('content.id

我想在Coffeescript中实现以下Javascript代码

App.ItemView = Ember.View.extend({
    classNameBindings: ['itemId'],
    itemId: function() {
        console.log(this.get('content'));
        return "item-%@".fmt(this.get('content.id'));
    }.property('content.id'),
    templateName: 'item'    
}); 
以下是我到目前为止在coffeescript中的内容:

App.ItemView = Ember.View.extend(
    classNameBindings: ['itemId']

    itemId: ->
        console.log this.get('content')
        contentId = this.get('content.id')
        "item-#{contentId}");
    .property('content.id')

    templateName: 'item'    
)
我得到:

Error: Parse error on line 11: Unexpected '.'
问题似乎出在
.property('content.id')中的点上

。我不知道这是怎么翻译成咖啡脚本的。如何在Coffeescript中正确实现此视图?

这已经花了很长时间,但我认为应该这样写:

App.ItemView = Ember.View.extend(
  classNameBindings: ['itemId']

  itemId: (->
    console.log this.get('content')
    contentId = this.get('content.id')
    "item-#{contentId}");
  ).property('content.id')

  templateName: 'item'    
)
您必须保护计算属性不受可能尚未定义的值的影响。也就是说,如果内容对象上已经有id属性,那么代码就可以了。如果内容未定义,那么您将无法查找其ID属性,您可能会看到投诉

你也可以使用

itemId: Ember.computed(->
  ..
).property('content.id')
观察者也有类似的模式。事实上,观察者也会在没有条件的情况下完成同样的事情:

itemId: null

contentIdChanged: (->
  @set 'itemId', 'item-%@'.fmt(@get 'content.id')
).observes('content.id')

我喜欢使用
Ember.computed

itemId: Ember.computed 'firstName', 'lastName', ->
  "#{@get('firstName')} #{@get('lastName')}"

哇,太快了!这个问题已经解决了,所以我接受了你的回答,但我认为我的代码不正确,我正试图找出如何正确访问
content.id
,因为现在它还没有定义。无论如何,转换为coffeescript的主要问题已经解决。非常感谢。你能在coffeescript中执行
'item-%@.fmt
吗?我记得我犯了一个错误,这在咖啡里看起来比那些古怪的帕伦斯好得多。
itemId: Ember.computed 'firstName', 'lastName', ->
  "#{@get('firstName')} #{@get('lastName')}"