问题
Any ideas on how to write this as coffeescript?
Person = Ember.Object.extend({
// these will be supplied by `create`
firstName: null,
lastName: null,
fullName: function() {
var firstName = this.get('firstName');
var lastName = this.get('lastName');
return firstName + ' ' + lastName;
}.property('firstName', 'lastName')
});
I'm particularly interested in the }.property
part of the code. I can't figure out how to write this in coffeescript.
回答1:
personally, i like braces around my functions:
Person = Ember.Object.extend(
firstName: null
lastName: null
fullName: (->
firstName = @get("firstName")
lastName = @get("lastName")
firstName + " " + lastName
).property("firstName", "lastName")
)
my head can better parse this ;-)
回答2:
first jsbeautifier it, then js2coffee it:
Person = Ember.Object.extend(
firstName: null
lastName: null
fullName: ->
firstName = @get("firstName")
lastName = @get("lastName")
firstName + " " + lastName
.property("firstName", "lastName")
)
As they say,make your code right.
来源:https://stackoverflow.com/questions/9205434/adding-a-function-call-on-the-end-of-function-in-coffeescript