Skip to content

beirigo/dispatcher-js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

Dispatcher

Dispatcher, formally known as Rails.js, is a small JavaScript library that automatically calls your functions when page is loaded. It was primary written to be used by Rails applications, but it will work with any web page you have.

Dispatcher requires jQuery.

Usage

Your page body must have a data attribute called page. This attribute should have the controller and action names.

<body data-page="users#index">

Rails developers can use the helper below:

def dispatcher_page_attribute
  controller_name = controller.class.name.underscore
  controller_name.gsub!(/\//, "_")
  controller_name.gsub!(/_controller$/, "")

  raw "#{controller_name}##{controller.action_name}"
end

<body data-page="<%= dispatcher_page_attribute %>"

Then all you need to do is include dispatcher.js

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dispatcher.js"></script>
<script type="text/javascript" src="lib.js"></script>

Now, you need to implement your own library. You must use a global variable called App.

App.users = {};

App.users["index"] = function() {
  // execute specific code for users/index
};

App.users["new"] = function() {
  // execute specific code for users/new
}

Dispatcher comes with some callbacks.

App.before = function() {};
App.after = function() {};
App.CONTROLLER.before = function() {};
App.CONTROLLER.after = function() {};

The chain is executed in this order:

App.before
App.CONTROLLER.before
App.CONTROLLER.ACTION
App.CONTROLLER.after
App.after

Dispatcher also detect browser’s user agent and set a body CSS class. If is IE, an additional body class identifying the version will be added.

Troubleshooting

If you’re migrating from Rails.js (the previous version), here’s what you’ll have to do:

  • Rename all Rails.CONTROLLER.ACTION references to App.CONTROLLER.ACTION.

  • Rename all remaining Rails references - if any - to Dispatcher.

  • Callbacks should be set directly on App.

License

(The MIT License)

Copyright © 2011 Nando Vieira (nandovieira.com.br)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Simple jQuery dispatcher for web apps.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 98.0%
  • CSS 2.0%