Spine.js,设置模型默认值

时间:2013-03-30 10:31:18

标签: coffeescript spine.js

在rails中我们有生命周期钩子,它允许我们这样做:

class Subscription < ActiveRecord::Base
  before_create :record_signup

  private
    def record_signup
      self.signed_up_on = Date.today
    end
end

Spine.js中有没有最好的方法来完成同样的事情(我需要设置一些默认值)?

目前我这样做,但也许存在更好的方式?

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

Subscription.bind 'beforeSave', Subscription.record_signup

2 个答案:

答案 0 :(得分:2)

CoffeeScript类主体是可执行的:

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

    @bind 'beforeSave', @record_signup

答案 1 :(得分:1)

如果未设置默认值,请覆盖标准Model.create函数怎么样?

@create: (atts, options) ->
  atts.myVal or= 'someDefault'
  record = new @(atts)
  record.save(options)