Permalink
Browse files

Refactored: moved jQuery plugin on demand loading into codebase and r…

…efactored examples to use that.
  • Loading branch information...
1 parent 68842ad commit 4dd588ecb2827abe68ef0c698666c24b6aea7f51 @mmarcon committed Jul 12, 2012
View
@@ -15,10 +15,13 @@
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
-.PHONY: all example
+.PHONY: all example jqueryui
all:
./scripts/closurize.sh
example:
- ./scripts/runexample.sh
+ ./scripts/runexample.sh basic
+
+jqueryui:
+ ./scripts/runexample.sh jquery-ui-ondemand
@@ -1,3 +1,3 @@
-(function(e){var c={fn2script:function(a){return a+"-ondemand.js"},jqueryPlugin:!1},f,b,g,i,d;f=function(a){this.options=e.extend({},c,a);this._defaults=c;this._name="onDemand";this._version="0.1";this._proxy={};this.options.jqueryPlugin&&(this._proxy=e.fn);d=this};b=f.prototype;b.fn2script=function(a){return this.options.fn2script(a)};b.firstLoad=function(a,j,b){var c=this.fn2script(a),h=e.Deferred(),f;if(g(d._proxy[a]))f=d._proxy[a].apply(b,j),h.resolve(f);else{if(!c)return;e.getScript(c).done(function(){var c;
-g(d._proxy[a])?c=d._proxy[a].apply(b,j):i(d._proxy[a])&&(c=d._proxy[a]);h.resolve(c)}).fail(function(){h.reject("OnDemand failed to load script "+c)})}return h.promise()};b.setOptions=function(a){this.options=e.extend({},c,a)};b.resetOptions=function(){this.options=e.extend({},c,{})};b.preload=function(a){this.firstLoad(a)};b.invoke=function(a,b,c){return this.firstLoad(a,b,c)};b.use=function(a){return this.firstLoad(a)};b.register=function(a,b){if(g(b)||i(b))d._proxy[a]=b};b.registerAll=function(a){for(var b in a)a.hasOwnProperty(b)&&
-this.register(b,a[b])};g=function(a){return"function"===typeof a};i=function(a){return"object"===typeof a};e.onDemand=new f(void 0)})(jQuery,window);
+(function(e){var c={fn2script:function(a){return a+"-ondemand.js"},jqueryPlugin:!1},g,b,h,j,d;g=function(a){this.options=e.extend({},c,a);this._defaults=c;this._name="onDemand";this._version="0.1";this._proxy={};d=this};b=g.prototype;b.fn2script=function(a){return this.options.fn2script(a)};b.firstLoad=function(a,f,b){var c=this.fn2script(a),i=e.Deferred(),g;if(h(d._proxy[a]))g=d._proxy[a].apply(b,f),i.resolve(g);else{if(!c)return;e.getScript(c).done(function(){var c;h(d._proxy[a])?c=d._proxy[a].apply(b,
+f):j(d._proxy[a])?c=d._proxy[a]:d.options.jqueryPlugin&&(c=e.fn[a].apply(b,f));i.resolve(c)}).fail(function(){i.reject("OnDemand failed to load script "+c)})}return i.promise()};b.internalPreload=function(a){var f=this.fn2script(a);h(d._proxy[a])||f&&e.getScript(f)};b.setOptions=function(a){this.options=e.extend({},c,a)};b.resetOptions=function(){this.options=e.extend({},c,{})};b.preload=function(a){this.internalPreload(a)};b.invoke=function(a,f,b){return this.firstLoad(a,f,b)};b.use=function(a){return this.firstLoad(a)};
+b.register=function(a,b){if(h(b)||j(b))d._proxy[a]=b};b.registerAll=function(a){for(var b in a)a.hasOwnProperty(b)&&this.register(b,a[b])};h=function(a){return"function"===typeof a};j=function(a){return"object"===typeof a};e.onDemand=new g(void 0)})(jQuery,window);
@@ -48,7 +48,7 @@
<div class="photo"></div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
- <script type="text/javascript" src="jquery.on.demand.min.js"></script>
+ <script type="text/javascript" src="jquery.on.demand.js"></script>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
File renamed without changes.
File renamed without changes.
@@ -8,9 +8,9 @@
<!-- jQuery -->
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<!-- onDemand -->
- <script type="text/javascript" src="js/jquery.on.demand.js"></script>
+ <script type="text/javascript" src="jquery.on.demand.js"></script>
<!-- loader -->
- <script type="text/javascript" src="js/ondemandloader.js"></script>
+ <script type="text/javascript" src="ondemandloader.js"></script>
<!--script type="text/javascript" src="js/jquery-ui-1.8.21.custom.min.js"></script-->
<script type="text/javascript">
$(window).load(function(){
@@ -1,149 +0,0 @@
-/*
- * Copyright (C) 2012 Massimiliano Marcon
-
- * 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.
- */
-
-;(function($, window, undefined) {
- 'use strict';
- var pluginName = 'onDemand',
- version = '0.1',
- defaults = {
- fn2script: function(fn) {
- return fn + '-ondemand.js';
- },
- jqueryPlugin: false
- },
- OnDemand,
- P, getInstance, isFn, isObj,
- that;
-
- OnDemand = function(options) {
- this.options = $.extend( {}, defaults, options);
-
- this._defaults = defaults;
- this._name = pluginName;
- this._version = version;
-
- this._proxy = {};
-
- that = this;
- };
-
- P = OnDemand.prototype;
-
- P.fn2script = function(fn) {
- return this.options.fn2script(fn);
- };
-
- P.firstLoad = function(fn, args, context){
- var script = this.fn2script(fn), deferred = $.Deferred(), rv;
-
- if (isFn(that._proxy[fn])) {
- rv = that._proxy[fn].apply(context, args);
- deferred.resolve(rv);
- }
- else {
- if (!script) {
- return;
- }
- $.getScript(script).done(function(){
- var rv;
- if (isFn(that._proxy[fn])) {
- rv = that._proxy[fn].apply(context, args);
- }
- else if (isObj(that._proxy[fn])) {
- rv = that._proxy[fn];
- }
- else if (that.options.jqueryPlugin) {
- rv = $.fn[fn].apply(context, args);
- }
- deferred.resolve(rv);
- }).fail(function(){
- deferred.reject('OnDemand failed to load script ' + script);
- });
- }
- return deferred.promise();
- };
-
- P.internalPreload = function(fn) {
- var script = this.fn2script(fn);
-
- if (isFn(that._proxy[fn])) {
- return;
- }
- else {
- if (!script) {
- return;
- }
- $.getScript(script);
- }
- };
-
- P.setOptions = function(options){
- this.options = $.extend( {}, defaults, options);
- };
-
- P.resetOptions = function(){
- this.options = $.extend( {}, defaults, {});
- };
-
- //@forTest
- P.reset = function(){
- delete $[pluginName];
- $[pluginName] = getInstance();
- };
- //@endForTest
-
- P.preload = function(fn){
- this.internalPreload(fn);
- };
- P.invoke = function(fn, args, context){
- return this.firstLoad(fn, args, context);
- };
- P.use = function(object){
- return this.firstLoad(object);
- };
- P.register = function(name, fn){
- if (isFn(fn) || isObj(fn)) {
- that._proxy[name] = fn;
- }
- };
- P.registerAll = function(object){
- var component;
- for (component in object) {
- if (object.hasOwnProperty(component)) {
- this.register(component, object[component]);
- }
- }
- };
-
- getInstance = function(options) {
- return new OnDemand(options);
- };
-
- isFn = function(what) {
- return typeof what === 'function';
- };
-
- isObj = function(what) {
- return typeof what === 'object';
- };
-
- $[pluginName] = getInstance();
-
-})(jQuery, window);
@@ -1,3 +0,0 @@
-(function(e){var c={fn2script:function(a){return a+"-ondemand.js"},jqueryPlugin:!1},f,b,g,i,d;f=function(a){this.options=e.extend({},c,a);this._defaults=c;this._name="onDemand";this._version="0.1";this._proxy={};this.options.jqueryPlugin&&(this._proxy=e.fn);d=this};b=f.prototype;b.fn2script=function(a){return this.options.fn2script(a)};b.firstLoad=function(a,j,b){var c=this.fn2script(a),h=e.Deferred(),f;if(g(d._proxy[a]))f=d._proxy[a].apply(b,j),h.resolve(f);else{if(!c)return;e.getScript(c).done(function(){var c;
-g(d._proxy[a])?c=d._proxy[a].apply(b,j):i(d._proxy[a])&&(c=d._proxy[a]);h.resolve(c)}).fail(function(){h.reject("OnDemand failed to load script "+c)})}return h.promise()};b.setOptions=function(a){this.options=e.extend({},c,a)};b.resetOptions=function(){this.options=e.extend({},c,{})};b.preload=function(a){this.firstLoad(a)};b.invoke=function(a,b,c){return this.firstLoad(a,b,c)};b.use=function(a){return this.firstLoad(a)};b.register=function(a,b){if(g(b)||i(b))d._proxy[a]=b};b.registerAll=function(a){for(var b in a)a.hasOwnProperty(b)&&
-this.register(b,a[b])};g=function(a){return"function"===typeof a};i=function(a){return"object"===typeof a};e.onDemand=new f(void 0)})(jQuery,window);
@@ -1,8 +1,6 @@
(function(){
var functions = ['accordion', 'dialog', 'tabs', 'datepicker', 'slider', 'progressbar'];
- //$('.myclass').dialog(options);
-
functions.forEach(function(f){
$.fn[f] = function(){
$.onDemand.invoke(f, arguments, this);
@@ -1,9 +1,9 @@
#!/bin/bash
-cp dist/jquery.on.demand.min.js examples
+cp src/jquery.on.demand.js examples/$1
-cd examples
-trap "{ rm -f jquery.on.demand.min.js; exit 0; }" EXIT
+cd examples/$1
+trap "{ rm -f jquery.on.demand.js; exit 0; }" EXIT
echo "Example is running at http://localhost:8000"
echo "^C to stop"
python -m SimpleHTTPServer &> /dev/null
@@ -40,9 +40,6 @@
this._version = version;
this._proxy = {};
- if (this.options.jqueryPlugin) {
- this._proxy = $.fn;
- }
that = this;
};
@@ -72,6 +69,15 @@
else if (isObj(that._proxy[fn])) {
rv = that._proxy[fn];
}
+ else if (that.options.jqueryPlugin) {
+ //I am loading on demand a jQuery plugin:
+ //this means that once the code is parsed it will add
+ //itself to jQuery's prototype, thus replacing the stub
+ //functions that where initially provided. Therefore
+ //on-demand won't be called again when that particular plugin
+ //or function is invoked.
+ rv = $.fn[fn].apply(context, args);
+ }
deferred.resolve(rv);
}).fail(function(){
deferred.reject('OnDemand failed to load script ' + script);
@@ -80,11 +86,22 @@
return deferred.promise();
};
+ P.internalPreload = function(fn) {
+ var script = this.fn2script(fn);
+
+ if (isFn(that._proxy[fn])) {
+ return;
+ }
+ else {
+ if (!script) {
+ return;
+ }
+ $.getScript(script);
+ }
+ };
+
P.setOptions = function(options){
this.options = $.extend( {}, defaults, options);
- if (this.options.jqueryPlugin) {
- this._proxy = $.fn;
- }
};
P.resetOptions = function(){
@@ -99,7 +116,7 @@
//@endForTest
P.preload = function(fn){
- this.firstLoad(fn);
+ this.internalPreload(fn);
};
P.invoke = function(fn, args, context){
return this.firstLoad(fn, args, context);

0 comments on commit 4dd588e

Please sign in to comment.