0

我有以下有效的 javascript:

App.Person = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
birthday: DS.attr('date'),

fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName');
  }.property('firstName', 'lastName')
});

根据js2coffee.org这个 js 相当于下面的咖啡脚本:

App.Person = DS.Model.extend(
  firstName: DS.attr("string")
  lastName: DS.attr("string")
  birthday: DS.attr("date")
  fullName: ->
    @get("firstName") + " " + @get("lastName")
  .property("firstName", "lastName")
)

然而,同样的咖啡脚本不会编译回有效的 javascript。它甚至不是有效的咖啡脚本,因为它会出现“意外”错误。"

如何编写有效的 Coffee Script 来创建与上面列出的相同或语法等效的 javascript?

4

2 回答 2

0

Coffeescript:

App.Person = DS.Model.extend(
  firstName: DS.attr("string")
  lastName: DS.attr("string")
  birthday: DS.attr("date")
  fullName: ( ->
    @get("firstName") + " " + @get("lastName")
  ).property("firstName", "lastName")
)

编译为:

App.Person = DS.Model.extend({
  firstName: DS.attr("string"),
  lastName: DS.attr("string"),
  birthday: DS.attr("date"),
  fullName: (function() {
    return this.get("firstName") + " " + this.get("lastName");
  }).property("firstName", "lastName")
});
于 2012-05-04T02:36:38.410 回答
0

反正有没有让它像这样:

class App.Person extends DS.Model
    name: DS.attr

我知道它不适用于这种语法,但我想使用 Coffee 的类语法

于 2014-04-11T01:19:41.093 回答