CoffeeScript class bodies are executable :
class Subscription extends Spine.Model
@record_signup: (self) ->
self.signed_up_on = new Date()
@bind 'beforeSave', @record_signup
Question
There is in rails we have lifecycle hooks, which allows us doing this:
class Subscription < ActiveRecord::Base
before_create :record_signup
private
def record_signup
self.signed_up_on = Date.today
end
end
Is there best way to accomplish same thing (i need it to set some default values) in Spine.js
?
Currently i doing it this way, but maybe there is better way exists ?
class Subscription extends Spine.Model
@record_signup: (self) ->
self.signed_up_on = new Date()
Subscription.bind 'beforeSave', Subscription.record_signup
La solution
CoffeeScript class bodies are executable :
class Subscription extends Spine.Model
@record_signup: (self) ->
self.signed_up_on = new Date()
@bind 'beforeSave', @record_signup
Autres conseils
How about overriding the standard Model.create function to include your default values if they aren't set?
@create: (atts, options) ->
atts.myVal or= 'someDefault'
record = new @(atts)
record.save(options)