diff --git a/whenever.js b/whenever.js index 97b5cab..2e7f333 100644 --- a/whenever.js +++ b/whenever.js @@ -1,8 +1,11 @@ -var whenever = function(element){ +var whenever = function (element) { + "use strict"; + var choose_element, choose_function, binding, is, given, then; + // Decide what element to bind to // if the element has been defined, use that // otherwise, use an anchor containing the text - var choose_element = function(element){ + choose_element = function (element) { if(typeof whenever.definitions[element] === 'string') { return whenever.definitions[element]; @@ -20,153 +23,166 @@ var whenever = function(element){ // choose a function from a collection of functions // returns the function to call as the first argument // and the arguments to call it with as the second - var choose_function = function(string_to_match, collection){ - var args = []; - var function_to_call; + choose_function = function (string_to_match, collection) { + var args, function_to_call, potential_match, matches; + args = []; if(typeof collection[string_to_match] === 'function') { function_to_call = collection[string_to_match]; } else { - for(var potential_match in collection) + for(potential_match in collection) { - var matches = string_to_match.match(new RegExp(potential_match)) - if(matches) + if(collection.hasOwnProperty(potential_match)) { - matches.shift(); - function_to_call = collection[potential_match]; - args = matches; + matches = string_to_match.match(new RegExp(potential_match)); + if(matches) + { + matches.shift(); + function_to_call = collection[potential_match]; + args = matches; + } } } } if(typeof function_to_call === 'function') { - return function(){ + return function () { return function_to_call.apply(this, args); - } + }; } else { throw("'" + string_to_match + "' was not found"); } - } + }; // A private object to store the element, the event and the action - var binding = { + binding = { selector: choose_element(element), conditions: [], actions: [], action: undefined }; - var is, given, then; // store the event and return an object for chaining - is = function(event){ + is = function (event) { if(whenever.translations[event]) { - binding.event = whenever.translations[event] + binding.event = whenever.translations[event]; } else { - throw(event + ' does not map to an event') + throw(event + ' does not map to an event'); } return { and: given, given: given, then: then - } - } + }; + }; // store a predicate along the chain - given = function(condition){ + given = function (condition) { - binding.conditions.push(condition) + binding.conditions.push(condition); // and and then are before we've called then return { and: given, then: then - } - } + }; + }; // call the binding function with condition as appropriate - then = function(action){ + then = function (action) { var function_to_apply; // add the action to the chain of actions - binding.actions.push(action) + binding.actions.push(action); // unbind the previous 'then' or 'and' - if(typeof binding.action === 'function' && binding.event != 'ready' && binding.event != 'load') + if(typeof binding.action === 'function' + && binding.event !== 'ready' + && binding.event !== 'load') { whenever.unbind_function_to_event( binding.selector, binding.event, binding.action - ) + ); } // create a function that runs the condition before running the actions - function_to_apply = function(){ - var out; - for(var i = 0; i