Autoexecution of javascript based on data attribute

gem install autoexec_bat -v 0.1.1



AutoexecBat was written to aid organizing and running javascript code using the Rails asset pipeline.

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 = ->
        $('table tbody td').on 'click', -> # event handler
        $('input').on 'click', -> # another event handler

    takeItAway = ->

    setupSomethingElse = ->

# A module with dependencies
define "App.Products.Show", ["App.Products"], (exports) ->
    exports.autoexec = ->
        # this module has a dependency to App.Products,
        # which will be executed first

# 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 ->

# If you're using the jQuery plugin you can fetch the callee in the autoexec function
define "App.Products.Show", (exports) ->
    exports.autoexec = (productItem) ->

    setupEventListeners = (productItem) ->
        $(productItem).on 'click', -> # specific event handler

# y u no got jquery?
# Run it manually or use whatever "App.Products.Show"
# or

# To push the callee to autoexec "App.Products.Show", productItem
# or

# If you want modules to be autoexec'd several times
    idempotent: false

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>


If you have a module that you want to be included always, you can instruct AutoexecBat to require it:

AutoexecBat.autoRequire = "App.My.Module"

Note: this will only work with modules defined with define().


Add this line to your application's Gemfile:

gem 'autoexec_bat'

Require your modules and activate autoexecution (with turbolinks - default in Rails 4):

#= require autoexec_bat
#= require_tree ./folder-containing-your-modules
jQuery(document).bind 'ready page:load', ->
        idempotent: false

For Rails 3:

#= require autoexec_bat
#= require_tree ./folder-containing-your-modules
jQuery ->


Install mocha and chai:

# npm install -g mocha coffee-script
# npm install chai jack

And run the test suite:

# mocha -R spec --watch     (or)
# rake test


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request