This repository has been archived by the owner on Feb 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Eric Hamilton
committed
Jul 31, 2012
1 parent
24c7d9d
commit a45bbc9
Showing
4 changed files
with
252 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.