Sha256: 15424e43958a9624ba7f2c8d2e2fe9e28a478ab5677ac51cae07e3cb4852b99b

Contents?: true

Size: 1.81 KB

Versions: 3

Compression:

Stored size: 1.81 KB

Contents

# Cached regex to split keys for `delegate`, from backbone.js.
delegateEventSplitter = /^(\S+)\s*(.*)$/

Ultimate.Backbone.ViewMixins.Nodes =

  nodes: null

  # Overwritten parent method Backbone.View.setElement() as hook for findNodes()
  setElement: (element, delegate) ->
    @undelegateEvents()  if @$el
    @$el = if element instanceof Backbone.$ then element else Backbone.$(element)
    @el = @$el[0]
    @findNodes() # inserted hook
    @delegateEvents()  if delegate isnt false
    @

  findNodes: (jRoot = @$el, nodes = @nodes) ->
    jNodes = {}
    nodes = @nodes.call(@)  if _.isFunction(nodes)
    if _.isObject(nodes)
      for nodeName, selector of nodes  when nodeName isnt 'selector'
        _isObject = _.isObject(selector)
        if _isObject
          nestedNodes = selector
          selector = nestedNodes['selector']
        jNodes[nodeName] = @[nodeName] = jRoot.find(selector)
        if _isObject
          _.extend jNodes, @findNodes(jNodes[nodeName], nestedNodes)
    jNodes

  # Overload and proxy parent method Backbone.View.delegateEvents() as hook for normalizeEvents().
  delegateEvents: (events) ->
    args = _.toArray(arguments)
    args[0] = @normalizeEvents(events)
    @__super 'delegateEvents', args

  # TODO docs
  normalizeEvents: (events) ->
    events = _.result(@, "events")  unless events
    if events
      normalizedEvents = {}
      for key, method of events
        [[], eventName, selector] = key.match(delegateEventSplitter)
        selector = _.result(@, selector)
        selector = selector.selector  if selector instanceof Backbone.$
        if _.isString(selector)
          selector = selector.replace(@$el.selector, '')  if _.string.startsWith(selector, @$el.selector)
          key = "#{eventName} #{selector}"
        normalizedEvents[key] = method
      events = normalizedEvents
    events

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ultimate-base-0.7.2 app/assets/javascripts/ultimate/backbone/view-mixins/nodes.js.coffee
ultimate-base-0.6.2 app/assets/javascripts/ultimate/backbone/view-mixins/nodes.js.coffee
ultimate-base-0.6.0 app/assets/javascripts/ultimate/backbone/view-mixins/nodes.js.coffee