Sha256: c5fc16a2cb7f7012fd17a2a2d5a016b3713f81c73ccbeea6ade09d6fda4ddb59

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

define ->

  class BaseController
    constructor: (action) ->
      # bind events from subclasses
      for event in ['click', 'focus', 'blur', 'change', 'keyup', 'submit']
        for selector, params of (this[event]? && this[event]())
          @bindevent selector, event, params

      # call action if it exists
      @action = action
      this[action]() if this[action]?

    bindevent: (selector, event, params) ->
      # extract function from params
      if $.type(params) == 'function'
        # direct call
        fnc = params
      else
        # at first parse the selector for restrictions
        restricted_to = params
        [selector, action_restriction] = selector.split('@')
        if action_restriction?
          if action_restriction == 'modify'
            restricted_to = ['create', 'update', 'edit', 'new']
          else
            restricted_to = [action_restriction]
        return unless @action in restricted_to
        fnc = params[params.length-1]

      # wrap our callback with event params
      wrapped = (e) =>
        fnc.apply(this, [$(e.target), e])

      # bind event to current and future selectors
      $(selector).livequery(event, wrapped)

    # syntax sugar for bindevent callbacks
    at: (page, fnc) ->
      [page, fnc]

    on: (params...) ->
      params

    on_modify: (fnc) ->
      ['create', 'update', 'edit', 'new', fnc]

  return BaseController

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
requirejs-controllers-0.1.1 vendor/assets/javascripts/controllers/base.js.coffee
requirejs-controllers-0.1.0 vendor/assets/javascripts/controllers/base.js.coffee