Skip to content

Instantly share code, notes, and snippets.

@brumm
Created November 8, 2012 13:09
Show Gist options
  • Save brumm/4038717 to your computer and use it in GitHub Desktop.
Save brumm/4038717 to your computer and use it in GitHub Desktop.
# -------------------------
# application_controller.rb
# -------------------------
class ApplicationController < ActionController::Base
before_filter :setup_gon
def setup_gon
Gon.global.controller = controller_name
Gon.global.action = action_name
end
end
# ----------------
# global.js.coffee
# ----------------
# only one dom-ready event to fire on
$ ->
# get controller and action names
controller = Appname.global.controller
action = Appname.global.action
# get namespace object, init and actual action method
namespace = Appname.controllers[controller]
init = namespace["init"]
method = namespace[action]
# bind 'this' to namespace to faciliate storing
# objects on the parent namespace, making
# them available to helpers and actions
init.call namespace if $.isFunction init
method.call namespace if $.isFunction method
# ----------------
# users.js.coffee
# ----------------
Appname.controllers =
users:
init: ->
@someSetting = {}
index: ->
doThing() if @someSetting?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment