2013-03-30 59 views
1

存在轨道,我们有生命周期挂钩,这让我们这样做:Spine.js,设置模式默认值

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

CoffeeScript的类主体是可执行的:

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

    @bind 'beforeSave', @record_signup 
+0

可以进一步简化为:'@bind'beforeSave',@ record_signup'? – Dfr

+0

它完全可以! – Ven

1

如何重写标准Model.create功能,包括默认值,如果他们没有设置?

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

这看起来是可行的,我们可以用单个“超级”线代替2条底线吗? – Dfr

+0

应该工作 – aeischeid