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')
});
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
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With