coffeescript 1.7打破了我的ember.js计算属性

Aar*_*oir 3 javascript coffeescript ember.js

鉴于以下coffeescript.

App.Whatever = Em.ArrayController.extend
  clean: Em.computed ->
    @get('content').filterBy('clean', true)
  .property 'content'
Run Code Online (Sandbox Code Playgroud)

Coffeescript <1.7会正确输出:

App.Whatever = Em.ArrayController.extend({
  clean: Ember.computed(function() {
    return this.get('content').filterBy('clean', true);
  }).property('content')
});
Run Code Online (Sandbox Code Playgroud)

现在Coffeescript 1.7输出:

 App.Whatever = Em.ArrayController.extend({
  clean: Ember.computed(function() {
    return this.get('content').filterBy('clean', true);
  })
 }).property('content');
Run Code Online (Sandbox Code Playgroud)

这似乎是一个外卖.我错过了什么或者我是否必须重写所有计算属性?

Bil*_*oon 8

我认为你对链式属性使用coffeescript的方式没有记录.我认为链接的官方方法是使用括号来明确定义属性的附加位置...

App.Whatever = Em.ArrayController.extend
  clean: Em.computed( ->
    @get('content').filterBy('clean', true)
  ).property 'content'
Run Code Online (Sandbox Code Playgroud)

或者,如果你真的想避免括号,你可以像这样写

App.Whatever = Em.ArrayController.extend
  clean:
    Em.computed ->
      @get('content').filterBy('clean', true)
    .property 'content'
Run Code Online (Sandbox Code Playgroud)

以上两个例子都编译成了

App.Whatever = Em.ArrayController.extend({
  clean: Em.computed(function() {
    return this.get('content').filterBy('clean', true);
  }).property('content')
});
Run Code Online (Sandbox Code Playgroud)

更新:CoffeeScript 1.7新功能......

来自文档...... Leading . now closes all open calls, allowing for simpler chaining syntax.

$ 'body'
.click (e) ->
  $ '.box'
  .fadeIn 'fast'
  .addClass '.active'
.css 'background', 'white'
Run Code Online (Sandbox Code Playgroud)

会输出......

$('body').click(function(e) {
  return $('.box').fadeIn('fast').addClass('.active');
}).css('background', 'white');
Run Code Online (Sandbox Code Playgroud)