Permalink
Browse files

Allow any module to pass a beforeRender promise array. Added timing t…

…ests. Added promises queue (to support beforeRender), exposed as applitude.queue(). Added .queue() tests. Updated license info for jquery.deferred-queue (in-lined).
  • Loading branch information...
1 parent 24c7d9d commit a45bbc9b031111c4d5eaeedfd5146b086ce378fe Eric Hamilton committed Jul 31, 2012
Showing with 252 additions and 113 deletions.
  1. +23 −0 LICENSE
  2. +2 −0 README.markdown
  3. +168 −110 applitude.js
  4. +59 −3 test/test.js
View
23 LICENSE
@@ -0,0 +1,23 @@
+Applitude is covered by The MIT License:
+
+Copyright (c) 2012 Eric Elliott
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+
+deferredQueue adapted from jquery.deferred-queue under the BSD License:
+
+Copyright 2011 Xavier Morel. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY XAVIER MOREL ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
@@ -1,5 +1,7 @@
# Applitude - Simple module management.
+Status - Applitude is in production use with millions of monthly active users. However, it is very new, and is in use by relatively few projects. It might be buggy. It might not work as expected. It definitely isn't well documented. Please feel free to kick the tires and contribute bug fixes, but for now, only experts who feel confident to debug issues and contribute bug fixes should attempt to use this in a production codebase.
+
Applitude is a simple module management solution that serves the following needs:
* **Namespacing**. Modules can only be registered once, in order to avoid duplicate code runs, and tricky associated bugs.
View
@@ -1,138 +1,196 @@
/**
* Applitude - Application namespacing and module management.
*
- * Relies on jQuery Deferred(), EventEmitter2, and odotjs
+ * Depends on jQuery, EventEmitter2, and odotjs
*
* Copyright (c) Eric Elliott 2012
* MIT License
+ * http://opensource.org/licenses/MIT
*/
/*global jQuery, EventEmitter2, odotjs */
-(function (root, $, o, events) {
+(function () {
'use strict';
- var namespace = 'applitude',
- debugLog = [],
- loadErrors = {},
-
- /**
- * Deferred utilities
- */
- deferred = $.Deferred,
- when = $.when,
- resolved = deferred().resolve().promise(),
- rejected = deferred().reject().promise(),
- app,
- register,
- stringToArray,
- addMixins,
- tryRender;
-
- stringToArray = function (input, pattern) {
- pattern = pattern || /\s*\,\s*/;
- return input.trim().split(pattern);
- };
-
- addMixins = function addMixins(module) {
- var mixins = stringToArray(module.mixins),
- backup = o.extend({}, module);
- mixins.forEach(function (mixin) {
- if (app[mixin]) {
- o.extend(module, app[mixin]);
+ var deferredQueue = {};
+
+ /**
+ * deferredQueue - a deferred / promise queue to manage
+ * asynchronous event timing.
+ *
+ * Adpated from jQuery.deferred-queue under a BSD License:
+ * https://bitbucket.org/masklinn/jquery.deferred-queue
+ */
+ (function ($) {
+ $.extend(deferredQueue, {
+ queue: function () {
+ var queueDeferred = $.Deferred(),
+ promises = 0,
+ promise,
+ resolve;
+
+ resolve = function resolve() {
+ if (--promises > 0) {
+ return;
+ }
+ setTimeout($.proxy(queueDeferred, 'resolve'), 0);
+ };
+
+ promise = $.extend(queueDeferred.promise(), {
+ push: function () {
+ promises += 1;
+ $.when.apply(null, arguments)
+ .then(resolve, $.proxy(queueDeferred, 'reject'));
+ return this;
+ }
+ });
+
+ if (arguments.length) {
+ promise.push.apply(promise, arguments);
+ }
+
+ return promise;
}
});
- return o.extend(module, backup);
- };
-
- app = function applitudeFunction(appNs, environment, options) {
- var whenPageLoaded = deferred(),
- beforeRenderOption = (options && options.beforeRender) || [],
- beforeRender = [whenPageLoaded].concat(beforeRenderOption),
- whenRenderReady = when.apply(null, beforeRender),
- tryRender;
-
- tryRender = function tryRender(module) {
- if (typeof module.render === 'function') {
- whenRenderReady.then(module.render);
- }
+ }(jQuery));
+
+ (function (root, $, o, events) {
+ var namespace = 'applitude',
+ debugLog = [],
+ loadErrors = {},
+
+ /**
+ * Deferred utilities
+ */
+ deferred = $.Deferred,
+ when = $.when,
+ resolved = deferred().resolve().promise(),
+ rejected = deferred().reject().promise(),
+ queue = deferredQueue.queue,
+ app,
+ register,
+ stringToArray,
+ addMixins,
+ whenRenderReady = queue();
+
+ stringToArray = function (input, pattern) {
+ pattern = pattern || /\s*\,\s*/;
+ return input.trim().split(pattern);
+ };
+
+ addMixins = function addMixins(module) {
+ var mixins = stringToArray(module.mixins),
+ backup = o.extend({}, module);
+ mixins.forEach(function (mixin) {
+ if (app[mixin]) {
+ o.extend(module, app[mixin]);
+ }
+ });
+ return o.extend(module, backup);
};
- register = function register(ns, module) {
- var whenLoaded;
- if (!app[ns]) {
- app[ns] = module;
+ app = function applitudeFunction(appNs, environment, options) {
+ var whenPageLoaded = deferred(),
+ beforeRenderOption = (options && options.beforeRender) || [],
+ beforeRender = [whenPageLoaded].concat(beforeRenderOption),
+ tryRender;
- if (module.mixins) {
- addMixins(module);
+ whenRenderReady.push.apply(whenRenderReady, beforeRender);
+
+ tryRender = function tryRender(module) {
+ if (typeof module.render === 'function') {
+ whenRenderReady.then(module.render);
}
+ };
+
+ register = function register(ns, module) {
+ var whenLoaded,
+ beforeRender = (module && module.beforeRender);
+
+ if (!app[ns]) {
+ app[ns] = module;
+
+ if (module.mixins) {
+ addMixins(module);
+ }
- // If load exists, try to load
- if (typeof module.load === 'function') {
- try {
- // If a promise is returned, wait for load to finish.
- whenLoaded = module.load();
- if (whenLoaded) {
- whenLoaded.done(function () {
+ // Delay global render until promise is fulfilled?
+ // Note, this will not work if render executes before
+ // this module loads.
+ if (beforeRender) {
+ whenRenderReady.push.apply(whenRenderReady, beforeRender);
+ }
+
+ // If load exists, try to load
+ if (typeof module.load === 'function') {
+ try {
+ // If a promise is returned, wait for load to finish.
+ whenLoaded = module.load();
+ if (whenLoaded && whenLoaded.done) {
+ whenLoaded.done(function () {
+ tryRender(module);
+ });
+ } else {
tryRender(module);
- });
- } else {
- tryRender(module);
+ }
+ } catch (loadError) {
+ loadErrors[ns] = loadError;
+ app.log('Error loading module: ', ns, loadError);
}
- } catch (loadError) {
- loadErrors[ns] = loadError;
- app.log('Error loading module: ', ns, loadError);
+ } else if (!loadErrors[ns]) {
+ // if .render() exists, try to render
+ tryRender(module);
}
- } else if (!loadErrors[ns]) {
- // if .render() exists, try to render
- tryRender(module);
+
+ } else {
+ app.log('Error: Module already registered: ', ns);
}
+ };
+
+ $(function () {
+ whenPageLoaded.resolve();
+ });
+ // aliases
+ events.trigger = events.emit;
+
+ o.extend(app, {
+ register: register,
+ environment: environment,
+ appNamespace: appNs,
+ options: options
+ }, events);
+
+ return app;
+ };
+
+ o.extend(app, {
+ deferred: deferred,
+ resolved: resolved,
+ rejected: rejected,
+ when: when,
+ queue: queue,
+ o: o.extend(o),
+ events: events,
+ debugLog: debugLog
+ });
+
+ app.log = function log() {
+ var debug = app.environment && app.environment.debug,
+ hasConsole = (window.console !== undefined) && console.log;
+ if (debug && hasConsole) {
+ console.log.apply(console, [].slice.call(arguments, 0));
} else {
- app.log('Error: Module already registered: ', ns);
+ debugLog.push(arguments);
}
};
- $(function () {
- whenPageLoaded.resolve();
- });
+ root[namespace] = app;
- // aliases
- events.trigger = events.emit;
+ }((typeof exports !== 'undefined')
+ ? exports
+ : window,
+ jQuery,
+ odotjs,
+ new EventEmitter2()));
- o.extend(app, {
- register: register,
- environment: environment,
- appNamespace: appNs,
- options: options
- }, events);
-
- return app;
- };
-
- o.extend(app, {
- deferred: deferred,
- resolved: resolved,
- rejected: rejected,
- when: when,
- o: o.extend(o),
- events: events,
- debugLog: debugLog
- });
-
- app.log = function log() {
- var debug = app.environment && app.environment.debug,
- hasConsole = (window.console !== undefined) && console.log;
- if (debug && hasConsole) {
- console.log.apply(console, [].slice.call(arguments, 0));
- } else {
- debugLog.push(arguments);
- }
- };
-
- root[namespace] = app;
-
-}((typeof exports !== 'undefined')
- ? exports
- : window,
- jQuery,
- odotjs,
- new EventEmitter2()));
+}());
Oops, something went wrong.

0 comments on commit a45bbc9

Please sign in to comment.