Search code examples
coffeescriptspine.js

Spine.js, setting model default values


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

Solution

  • CoffeeScript class bodies are executable :

    class Subscription extends Spine.Model
        @record_signup: (self) ->
          self.signed_up_on = new Date()
    
        @bind 'beforeSave', @record_signup