Module: JsHooks::Controller::LocalInstanceMethods

Defined in:
lib/js_hooks/controller.rb

Instance Method Summary collapse

Instance Method Details

#add_js_hook(*args) ⇒ Object

Add javascript components to execute inside a document ready jquery closure before the body close tag in your templates. For each hook added, an object with the same name is searched and the init method is called. A suffix can be used with the init method to refine the initialization process:

add_js_hook( :users, :clients )
  --> Users.init(); Clients.init()

add_js_hook( :users, :clients, users: 'action' )
  --> Users.init(); Clients.init(); Users.initAction();

add_js_hook( :clients, users: [:default, 'action1', 'action2'] )
  --> Clients.init(); Users.init(); Users.initAction1(); Users.initAction2();

Three hooks are added by default: one with ‘application’ as name, one with the controller as name and one last with the controller as name and template as suffix. So, by default, the following methods will be called if they exist:

GET /users/1 --> Application.init(); Users.init(); Users.initShow()

A hook can be deleted by passing a false value as suffix

GET /users/1
add_js_hook( :clients, users: false)
  --> Application.init(); Clients.init();


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/js_hooks/controller.rb', line 47

def add_js_hook(*args)
  opts = args.extract_options!

  # Default init method
  args.each do |hook|
    hook = hook.to_s.camelize
    js_hooks[hook] ||= []
    js_hooks[hook] = (js_hooks[hook] + [PREFIX]).uniq
  end

  # Suffix init method
  opts.each_pair do |hook, methods|
    hook = hook.to_s.camelize
    js_hooks[hook] ||= []
    remove_default = false
    if methods
      methods = [methods].flatten.map! do |m|
        remove_default ||= !m
        m = '' if m == :default
        "#{PREFIX}#{m.to_s.camelize}"
      end
      js_hooks[hook] = (js_hooks[hook] + methods).uniq
      js_hooks[hook].delete(PREFIX) if remove_default
    else
      js_hooks.delete(hook)
    end
  end
end

#js_hooksObject

Javascript on demand hooks



20
21
22
# File 'lib/js_hooks/controller.rb', line 20

def js_hooks
  @js_hooks ||= {}
end