【问题标题】:coffeescript 1.7 broke my ember.js computed propertiescoffeescript 1.7 破坏了我的 ember.js 计算属性
【发布时间】:2014-02-25 01:10:06
【问题描述】:

鉴于以下咖啡脚本。

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

Coffeescript

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

现在 Coffeescript 1.7 输出:

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

这似乎是一个外卖。我是否遗漏了任何内容,还是必须重写所有计算属性?

Example

【问题讨论】:

    标签: javascript ember.js coffeescript


    【解决方案1】:

    我认为您将咖啡脚本用于链式属性的方式未记录在案。我相信链接的官方方法是使用括号来明确定义属性的附加位置......

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

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

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

    上面的两个例子都编译成

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

    更新: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'
    

    会输出...

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

    【讨论】:

      猜你喜欢
      • 2013-11-29
      • 2013-06-29
      • 1970-01-01
      • 2017-12-18
      • 2012-07-27
      • 2011-06-23
      • 2012-09-28
      • 2020-12-10
      • 1970-01-01
      相关资源
      最近更新 更多