Sha256: ea8f17f9eeb83e53571f22b8f83f10c24c25c88f67ad673b73449d2eb9989184

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

Luca.concerns.CollectionEventBindings = 
  __initializer: ()->
    Luca.concerns.CollectionEventBindings.__setup.call(@)

    unless @collectionEventBindingsSetup is true
      if Luca.isBackboneCollection(@collection)
        @collection.on "reset",         relayAs("collection:reset"), @
        @collection.on "add",           relayAs("collection:add"), @
        @collection.on "remove",        relayAs("collection:remove"), @
        @collection.on "change",        relayAs("collection:change"), @
        @collection.on "before:fetch",  relayAs("collection:fetch"), @
        
      @collectionEventBindingsSetup = true

  __setup: ()->
    return if _.isEmpty( @collectionEvents )

    manager = @collectionManager || Luca.CollectionManager.get()

    for signature, handler of @collectionEvents
      [key,eventTrigger] = signature.split(" ")

      collection = manager.getOrCreate( key )

      if !collection
        throw "Could not find collection specified by #{ key }"

      if _.isString(handler)
        handler = @[handler]

      unless _.isFunction(handler)
        throw "invalid collectionEvents configuration"

      try
        collection.on(eventTrigger, handler, @)
      catch e
        console.log "Error Binding To Collection in registerCollectionEvents", @
        throw e


relayAs = (eventName)->
  (args...)->
    args.unshift(eventName)
    @trigger.apply(@, args)

Version data entries

1 entries across 1 versions & 1 rubygems

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