Sha256: 7434f2ce0447a00a2cf8b79aec2240ee6daf95232e6334c883547784cd84762f

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

Luca.concerns.StateModel =
  __initializer: ()->
    @stateful = @stateAttributes if @stateAttributes?

    return unless @stateful?
    return if @state? 
    
    if _.isObject(@stateful) and not @defaultState?
      @defaultState = @stateful 

    @state = new Backbone.Model(@defaultState || {})
    @set = _.bind(@state.set, @state) 
    @get = _.bind(@state.get, @state) 

    for key, value of @state.toJSON()
      hook = "on" + _.str.capitalize(key) + "Change"
      getter = "get" + _.str.capitalize(key) 
      unless _.isFunction(@[getter])
        1
        #console.log("State Change Getter", getter)
        # @[getter] = ()=> @state.get(key)
        # WE COULD CREATE AUTO GETTERS HERE

      if _.isFunction(@[hook])
        1
        #console.log("State Change Hook", hook)
        # @stateChangeEvents[ key ] = hook
        # WE COULD AUTO BIND TO STATE CHANGE EVENTS HERE

    unless _.isEmpty(@stateChangeEvents)
      for attribute, handler of @stateChangeEvents
        fn = if _.isString(handler) then @[handler] else handler 
        
        if attribute is "*"
          @on "state:change", fn, @
        else
          @on "state:change:#{ attribute }", fn, @

    @state.on "change", (state)=>
      @trigger "state:change", state

      for changed, value of state.changedAttributes()
        @trigger "state:change:#{ changed }", state, value, state.previous(changed)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
luca-0.9.8 app/assets/javascripts/luca/concerns/state_model.coffee