无法将js转换为coffeescript

时间:2013-11-10 00:13:01

标签: ember.js coffeescript

我正在尝试学习ember.js(使用coffeescript),但教程是在js中,我遇到了一些我无法弄清楚如何进入coffeescript的代码。

我很确定问题来自我如何包装.property,但不知道如何解决它。谢谢你的帮助!

js代码(来自http://emberjs.com/guides/getting-started/marking-a-model-as-complete-incomplete/):

Todos.TodoController = Ember.ObjectController.extend({
  isCompleted: function(key, value){
    var model = this.get('model');

    if (value === undefined) {
      return model.get('isCompleted');
    } else {
      model.set('isCompleted', value);
      model.save();
      return value;
    }
  }.property('model.isCompleted')
});

我的coffeescript收到错误(也来自Js2coffee):

Todos.TodoController = Ember.ObjectController.extend(
    isCompleted: (key, value) ->
        model = @get("model")
        if value is `undefined`
            model.get "isCompleted"
        else
            model.set "isCompleted", value
            model.save()
            value
    .property("model.isCompleted")
)

1 个答案:

答案 0 :(得分:2)

CoffeeScript中的Parens是可选的,除非您需要它们。

Todos.TodoController = Ember.ObjectController.extend(
    # add paren before function
    isCompleted: ((key, value) ->
        model = @get("model")
        if value is `undefined`
            model.get "isCompleted"
        else
            model.set "isCompleted", value
            model.save()
            value
    ).property("model.isCompleted")
)