Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a method on a function definition in coffeescript

How would you translate this snippet of javascript to coffeescript? Specifically I'm struggling with how to call .property() on the function definition.

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')
});
like image 849
Josh Rickard Avatar asked Dec 11 '11 14:12

Josh Rickard


2 Answers

I think this is how you're supposed to write it:

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')
}

checkout this link

like image 173
alessioalex Avatar answered Oct 07 '22 13:10

alessioalex


There are a couple ways to define computed properties. Here are examples of each:

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')
like image 39
ebryn Avatar answered Oct 07 '22 12:10

ebryn