Skip to content
Browse files

Update documentation for Rails 4 / turbolinks

  • Loading branch information...
1 parent d87e48b commit 6ec460249740bf5b65bd852f1cad0b0ed191d0ba @theodorton theodorton committed Nov 2, 2012
Showing with 19 additions and 13 deletions.
  1. +19 −13 README.md
View
32 README.md
@@ -5,28 +5,28 @@ AutoexecBat was written to aid organizing and running javascript code using the
The main idea is to organize all javascript code into separate namespaces and let a data-autoexec attribute determine which module to run.
Some examples:
-
+
define "App.Products", (exports) ->
exports.autoexec = ->
# this is the autoexec function that AutoexecBat looks for
-
+
# Module with private methods
define "App.Products.Index", (exports) ->
exports.autoexec = ->
setupEventListeners()
setupSomethingElse()
takeItAway()
-
+
setupEventListeners = ->
$('table tbody td').on 'click', -> # event handler
$('input').on 'click', -> # another event handler
-
+
takeItAway = ->
$('tag').doSomething()
setupSomethingElse = ->
$('.private').show()
-
+
# A module with dependencies
define "App.Products.Show", ["App.Products"], (exports) ->
exports.autoexec = ->
@@ -35,30 +35,30 @@ Some examples:
# If all you want is to run the dependencies:
define "App.Gallery", ["App.UI.Fancybox", "App.UI.FileUpload"]
-
+
# A simple coffeescript class:
class namespace("App.Models").Product
# this class will be known as App.Models.Product
# Run AutoexecBat on all tags with a data-autoexec attribute (using jQuery)
jQuery ->
$('[data-autoexec]').autoexec()
-
- # y u no got jquery?
+
+ # y u no got jquery?
# Run it manually or use whatever
AutoexecBat.run "App.Products.Show"
- # or
+ # or
App.Products.Show.autoexec()
Finally, just add the module name to a data-autoexec attribute:
<body data-autoexec="App.Products.Index">
-
+
If the given module doesn't exist, it tries to execute a parent module.
In this case, App.Products.autoexec() will be executed:
<div data-autoexec="App.Products.Dev.Null">..</div>
-
+
### Auto-require
If you have a module that you want to be included always, you can instruct AutoexecBat to require it:
@@ -73,13 +73,19 @@ Add this line to your application's Gemfile:
gem 'autoexec_bat'
-Require your modules and activate autoexecution:
+Require your modules and activate autoexecution (with turbolinks - default in Rails 4):
#= require autoexec_bat
#= require_tree ./folder-containing-your-modules
- jQuery ->
+ jQuery(document).bind 'ready page:change', ->
$('[data-autoexec]').autoexec()
+For Rails 3:
+
+ #= require autoexec_bat
+ #= require_tree ./folder-containing-your-modules
+ jQuery ->
+ $('[data-autoexec]').autoexec()
## Contributing

0 comments on commit 6ec4602

Please sign in to comment.
Something went wrong with that request. Please try again.