Permalink
Browse files

Add idempotent option and maintain callee functionality

idempotent is true by default
  • Loading branch information...
1 parent 8b117dc commit 49f6f557f26d8b26f532fac2e0bbe3d82dda7401 @theodorton theodorton committed Nov 12, 2012
Showing with 32 additions and 20 deletions.
  1. +18 −11 autoexec_bat.js
  2. +14 −9 lib/assets/javascripts/autoexec_bat.coffee
View
@@ -1,5 +1,6 @@
// Generated by CoffeeScript 1.3.1
(function() {
+ // Generated by CoffeeScript 1.3.3
var $, AutoexecBat, root,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
@@ -41,15 +42,17 @@
target.loaded = false;
target.autoexec = function() {};
target.dependencies = dependencies;
- target.init = function() {
- if (!this.loaded) {
+ target.init = function(options) {
+ if (!(options.idempotent && this.loaded)) {
require(dependencies);
if (typeof this.autoexec === 'function') {
- this.autoexec();
+ this.autoexec(options.callee);
+ }
+ if (options.idempotent) {
+ this.autoexec = function() {
+ return AutoexecBat.log("Module already initialized");
+ };
}
- this.autoexec = function() {
- return AutoexecBat.log("Module already initialized");
- };
return this.loaded = true;
}
};
@@ -96,17 +99,17 @@
}
return module;
},
- initializeModule: function(nameOrModule) {
+ initializeModule: function(nameOrModule, options) {
var module;
module = typeof nameOrModule === "string" ? AutoexecBat.findModule(nameOrModule) : nameOrModule;
if (typeof module !== 'undefined' && typeof module.init !== 'undefined') {
- return module.init();
+ return module.init(options);
}
},
- run: function(name) {
+ run: function(name, options) {
var module;
module = AutoexecBat.findModule(name);
- return AutoexecBat.initializeModule(module);
+ return AutoexecBat.initializeModule(module, options);
}
};
@@ -122,8 +125,12 @@
$ = jQuery;
$.fn.extend({
autoexec: function(options) {
+ options = $.extend({
+ callee: this,
+ idempotent: true
+ }, options);
return this.each(function() {
- return AutoexecBat.run($(this).data('autoexec'));
+ return AutoexecBat.run($(this).data('autoexec'), options);
});
}
});
@@ -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 49f6f55

Please sign in to comment.