Permalink
Browse files

Merge pull request #2 from theodorton/idempotency

(non) Idempotency
  • Loading branch information...
gudleik committed Mar 7, 2013
2 parents 8b117dc + 762fd6b commit 6061a4e5e8f93e77311dbe992fe57b696e9ec291
Showing with 39 additions and 22 deletions.
  1. +7 −2 README.md
  2. +18 −11 autoexec_bat.js
  3. +14 −9 lib/assets/javascripts/autoexec_bat.coffee
View
@@ -63,6 +63,10 @@ Some examples:
# or
App.Products.Show.autoexec(productItem)
# If you want modules to be autoexec'd several times
App.Products.Show.autoexec
idempotent: false
Finally, just add the module name to a data-autoexec attribute:
<body data-autoexec="App.Products.Index">
@@ -90,8 +94,9 @@ Require your modules and activate autoexecution (with turbolinks - default in Ra
#= require autoexec_bat
#= require_tree ./folder-containing-your-modules
jQuery(document).bind 'ready page:change', ->
$('[data-autoexec]').autoexec()
jQuery(document).bind 'ready page:load', ->
$('[data-autoexec]').autoexec
idempotent: false
For Rails 3:
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -28,11 +28,12 @@ AutoexecBat =
target.loaded = false
target.autoexec = -> # make sure the autoexec function exists
target.dependencies = dependencies
target.init = (callee) ->
unless @loaded
target.init = (options) ->
unless options.idempotent and @loaded
require dependencies
@autoexec(callee) if typeof @autoexec is 'function'
@autoexec = -> AutoexecBat.log "Module already initialized"
@autoexec(options.callee) if typeof @autoexec is 'function'
if options.idempotent
@autoexec = -> AutoexecBat.log "Module already initialized"
@loaded = true
block target, top
@@ -55,13 +56,13 @@ AutoexecBat =
module = module[item] unless typeof module[item] is 'undefined'
module
initializeModule: (nameOrModule, callee) ->
initializeModule: (nameOrModule, options) ->
module = if typeof nameOrModule is "string" then AutoexecBat.findModule(nameOrModule) else nameOrModule
module.init(callee) if typeof module isnt 'undefined' and typeof module.init isnt 'undefined'
module.init(options) if typeof module isnt 'undefined' and typeof module.init isnt 'undefined'
run: (name, callee) ->
run: (name, options) ->
module = AutoexecBat.findModule name
AutoexecBat.initializeModule module, callee
AutoexecBat.initializeModule module, options
# Globals
@@ -75,4 +76,8 @@ unless typeof jQuery is 'undefined'
$ = jQuery
$.fn.extend
autoexec: (options) ->
return @each -> AutoexecBat.run $(@).data('autoexec'), @
options = $.extend
callee: @
idempotent: true
, options
return @each -> AutoexecBat.run $(@).data('autoexec'), options

0 comments on commit 6061a4e

Please sign in to comment.