Permalink
Browse files

Merge remote-tracking branch 'upstream/master'

  • Loading branch information...
2 parents 85a2879 + 104efba commit 111cfd46e251d73d18637cf1c8c30111a48d4063 @felipemorais committed Nov 16, 2012
Showing with 89 additions and 112 deletions.
  1. +85 −104 dist/basket.js
  2. +4 −8 dist/basket.min.js
View
@@ -1,61 +1,64 @@
-/*!
-* basket.js
-* v0.2.0 - 2012-11-14
+/*! basket.js - v0.2.0 - 3/9/2012
* http://addyosmani.github.com/basket.js
-* (c) Addy Osmani; MIT License
-* Created by: Addy Osmani, Sindre Sorhus, Andrée Hansson
-* Contributors: Ironsjp, Mathias Bynens, Rick Waldron
-*/
-
-(function( window, document ) {
- 'use strict';
-
- var head = document.head || document.getElementsByTagName('head')[0];
- var storagePrefix = 'basket-';
- var scripts = [];
- var scriptsExecuted = 0;
- var globalWaitCount = 0;
- var waitCallbacks = [];
-
- var isFunc = function( fn ) {
- return {}.toString.call( fn ) === '[object Function]';
- };
+* Copyright (c) 2012 Addy Osmani; Licensed MIT, GPL
+* Credits: Addy Osmani, Ironsjp, Mathias Bynens, Rick Waldron, Sindre Sorhus, Andrée Hansson */
+
+;(function ( window, document ) {
+ "use strict";
+
+ var
+ storagePrefix = "basket-",
+ scripts = [],
+ scriptsExecuted = 0,
+ waitCount = 0,
+ waitCallbacks = [],
- var getUrl = function( url, callback ) {
+ getUrl = function( url, callback ) {
var xhr = new XMLHttpRequest();
- xhr.open( 'GET', url );
+ xhr.open( "GET", url, true );
xhr.onreadystatechange = function() {
- if ( xhr.readyState === 4 && xhr.status === 200 ) {
+ if ( xhr.readyState === 4 ) {
callback( xhr.responseText );
}
};
xhr.send();
- };
+ },
- var saveUrl = function( url, key, expire, callback ) {
+ saveUrl = function( url, key, callback ) {
getUrl( url, function( text ) {
- var storeObj = wrapStoreData( text, expire );
- localStorage.setItem( key, JSON.stringify( storeObj ) );
+ localStorage.setItem( key, text );
- if ( isFunc( callback ) ) {
- callback( text );
+ if ( isFunc(callback) ) {
+ callback();
}
});
- };
+ },
+
+ isFunc = function( func ) {
+ return Object.prototype.toString.call( func ) === "[object Function]";
+ },
+
+ injectScript = function( text ) {
+ var
+ script = document.createElement("script"),
+ head = document.head || document.getElementsByTagName("head")[ 0 ];
- var injectScript = function( text ) {
- var script = document.createElement('script');
script.defer = true;
// Have to use .text, since we support IE8,
// which won't allow appending to a script
script.text = text;
+
head.appendChild( script );
- };
+ },
- var queueExec = function( waitCount, doExecute ) {
- var i, j, script, callback;
+ queueExec = function( waitCount ) {
+ var
+ i,
+ j,
+ script,
+ callback;
if ( scriptsExecuted >= waitCount ) {
for ( i = 0; i < scripts.length; i++ ) {
@@ -67,11 +70,7 @@
}
scripts[ i ] = null;
-
- if ( doExecute ) {
- injectScript( script );
- }
-
+ injectScript( script );
scriptsExecuted++;
for ( j = i; j < scriptsExecuted; j++ ) {
@@ -86,73 +85,68 @@
}
};
- var wrapStoreData = function( data, expiration ) {
- var now = +new Date();
- var storeObj = {
- data: data,
- stamp: now
- };
-
- if ( expiration ) {
- storeObj.expire = now + ( expiration * 60 * 60 * 1000 );
- }
-
- return storeObj;
- };
-
- var handleStackObject = function( obj ) {
- var key = storagePrefix + ( obj.key || obj.url );
- var waitCount = globalWaitCount;
- var scriptIndex = scripts.length;
- var source = JSON.parse( localStorage.getItem( key ) );
-
- var callback = function( text ) {
- scripts[ scriptIndex ] = text;
- queueExec( waitCount, obj.execute );
- };
-
- if ( !obj.url ) {
- return;
- }
+ window.basket = {
+ require: function ( uri, options ) {
+ options = options || {};
+
+ var
+ localWaitCount = waitCount,
+ scriptIndex = scripts.length,
+ key = storagePrefix + ( options.key || uri ),
+ source = localStorage.getItem( key );
+
+ scripts[ scriptIndex ] = null;
+
+ if ( source ) {
+ scripts[ scriptIndex ] = source;
+ queueExec( localWaitCount );
+ } else {
+ getUrl( uri, function( text ) {
+ localStorage.setItem( key, text );
+ scripts[ scriptIndex ] = text;
+ queueExec( localWaitCount );
+ });
+ }
- obj.execute = obj.execute !== false;
- scripts[ scriptIndex ] = null;
+ return this;
+ },
- if ( source && ( source.expire && source.expire - +new Date() > 0 ) ) {
- callback( source.data );
- } else {
- saveUrl( obj.url, key, obj.expire, callback );
- }
+ add: function( uri, options, callback ) {
+ options = options || {};
- if ( isFunc( obj.wait ) ) {
- basket.wait( obj.wait );
- }
- };
+ var key = storagePrefix + ( options.key || uri );
- window.basket = {
- require: function() {
- var i, l;
+ // default is to overwrite
+ if ( typeof options.overwrite === "undefined" ) {
+ options.overwrite = true;
+ }
- for ( i = 0, l = arguments.length; i < l; i++ ) {
- handleStackObject( arguments[ i ] );
+ // if they key exists and overwrite true, overwrite
+ if ( localStorage.getItem(key) ) {
+ if( options.overwrite ) {
+ saveUrl( uri, key, callback );
+ }
+ } else {
+ //key doesnt exist, add key as new entry
+ saveUrl( uri, key, callback );
}
return this;
},
remove: function( key ) {
localStorage.removeItem( storagePrefix + key );
+
return this;
},
wait: function( callback ) {
- globalWaitCount = scripts.length - 1;
-
- if ( isFunc( callback ) ) {
- if ( scriptsExecuted > globalWaitCount ) {
+ waitCount = scripts.length - 1;
+ if ( callback ) {
+ if ( scriptsExecuted > waitCount ) {
callback();
} else {
- waitCallbacks[ globalWaitCount ] = callback;
+ waitCallbacks[ waitCount ] = callback;
}
}
@@ -161,20 +155,7 @@
get: function( key ) {
return localStorage.getItem( storagePrefix + key ) || null;
- },
-
- clear: function() {
- var key;
- var ls = localStorage;
-
- for ( key in ls ) {
- if ( key.indexOf( storagePrefix ) === 0 ) {
- delete ls[ key ];
- }
- }
-
- return this;
}
};
-})( this, document );
+}( this, document ));
View
Oops, something went wrong.

0 comments on commit 111cfd4

Please sign in to comment.