在coffeescript中调用函数定义的方法

Jos*_*ard 10 javascript coffeescript ember.js

你会如何将这段javascript翻译成coffeescript?具体来说,我正在努力解决如何调用.property()函数定义.

MyApp.president = SC.Object.create({
  firstName: "Barack",
  lastName: "Obama",

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

    // Call this flag to mark the function as a property
  }.property('firstName', 'lastName')
});
Run Code Online (Sandbox Code Playgroud)

ale*_*lex 21

我想这就是你应该写的:

MyApp.president = SC.Object.create {
  firstName: "Barack",
  lastName: "Obama",
  fullName: (-> 
    return @get 'firstName' + ' ' + @get 'lastName'
    # Call this flag to mark the function as a property
  ).property('firstName', 'lastName')
}
Run Code Online (Sandbox Code Playgroud)

结帐此链接


ebr*_*ryn 5

有几种方法可以定义计算属性.以下是每个例子:

MyApp.president = Ember.Object.create
  firstName: "Barack"
  lastName: "Obama"
  fullName: (-> 
    @get 'firstName' + ' ' + @get 'lastName'
  ).property('firstName', 'lastName')

MyApp.president = Ember.Object.create
  firstName: "Barack"
  lastName: "Obama"
  fullName: Ember.computed(-> 
    @get 'firstName' + ' ' + @get 'lastName'
  ).property('firstName', 'lastName')
Run Code Online (Sandbox Code Playgroud)