Permalink
Browse files

Added a localStorage class, and fixed Story to use data() correctly

  • Loading branch information...
1 parent b63eec5 commit 46984aa007bea4ff7d72281d6b1cfe73dd6e4577 @curiousdannii committed Feb 20, 2012
Showing with 75 additions and 25 deletions.
  1. +21 −17 src/parchment/library.js
  2. +5 −6 src/parchment/model.js
  3. +49 −2 src/parchment/storage.js
@@ -52,24 +52,27 @@ Story = Model.subClass( 'Story', {
// Get the story file from storage, or download it
load: function()
{
- var data = this.data(),
- _deferred = this._deferred = $.Deferred();
+ var self = this,
+ _deferred = self._deferred = $.Deferred();
- // We have the data, so resolve the Deferred
- if ( data )
- {
- // Fake an XHR, all we access are these properties
- deferred.resolve({
- responseText: data,
- responseArray: this._array || ( this._array = file.text_to_array( data ) )
- });
- }
-
- // Otherwise download the file
- else
- {
- this.download();
- }
+ // Fetch the data if we can
+ this.data( function( data ) {
+ // We have the data, so resolve the Deferred
+ if ( data )
+ {
+ // Fake an XHR, all we access are these properties
+ _deferred.resolve({
+ responseText: data,
+ responseArray: self._array || ( self._array = file.text_to_array( data ) )
+ });
+ }
+
+ // Otherwise download the file
+ else
+ {
+ self.download();
+ }
+ });
return _deferred;
},
@@ -92,6 +95,7 @@ Story = Model.subClass( 'Story', {
self._deferred.resolve( jqXHR );
// Save the data to storage
self.data( jqXHR.responseText );
+ self._array = jqXHR.responseArray;
})
// We couldn't download the file - but offer the option to try again
.fail( function(){
@@ -3,15 +3,15 @@
A simple Model and ORM system
=============================
-Copyright (c) 2011 The Parchment Contributors
+Copyright (c) 2011-2012 The Parchment Contributors
BSD licenced
http://code.google.com/p/parchment
*/
/*
-After exploring some JS MVC options I've decided to try writing my own. I'm not opposed to using another, and might consider more options again in the future, if this proves insufficient.
+After exploring some JS ORM options I've decided to write my own. It's very basic but it gets the job done. Not bad for 200 lines of code.
TODO:
Model.init calls fetch but isn't passed a callback itself, does this matter?
@@ -117,15 +117,15 @@ var Model = Object.subClass({
}
}),
-// Collections are Object groups of Model instances
+// Collections are Arrays of Model instances
Collection = Object.subClass.call( Array, {
init: function( Class, parent )
{
this.Class = Class;
this._parent = parent;
},
- // Get the collection index from localStorage
+ // Get the collection index from storage
// note: will first empty the collection
fetch: function( callback, index )
{
@@ -147,13 +147,12 @@ Collection = Object.subClass.call( Array, {
else
{
storage.get( 'INDEX' + this.Class.Class, function( data ) {
- // Is the [] needed here?
self.fetch( callback, data || [] );
});
}
},
- // Save the Collection back to localStorage
+ // Save the Collection back to storage
// Note does not save collection members
save: function()
{
@@ -23,8 +23,9 @@ var storage_factory = (function(){
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB,
localStorage = window.localStorage,
-//IndexedDB class
+// IndexedDB class
IndexedDBClass = Object.subClass({
+ persist: 1,
init: function( db, callback )
{
var self = this;
@@ -90,10 +91,51 @@ IndexedDBClass = Object.subClass({
}
}),
+// LocalStorage/fake storage class
+LocalStorageClass = Object.subClass({
+ init: function( name )
+ {
+ this.name = name;
+ // If we don't have localStorage then fake it, but mark us as non-persistant
+ this.storage = localStorage || {};
+ this.persist = !!localStorage;
+ },
+ get: function( key, callback, keyprefix )
+ {
+ var rJSON = /^[[{]/,
+ data,
+ dataobj = {},
+ i = 0;
+
+ // Get several keys at once
+ if ( keyprefix != undefined )
+ {
+ // Make a request for each key
+ while ( i < key.length )
+ {
+ data = this.storage[this.name + keyprefix + key[i]];
+ dataobj[key[i++]] = rJSON.test( data ) ? JSON.parse( data ) : data;
+ }
+ callback( dataobj );
+ }
+ // Or just one
+ else
+ {
+ data = this.storage[this.name + key];
+ callback( rJSON.test( data ) ? JSON.parse( data ) : data );
+ }
+ },
+ set: function( key, value )
+ {
+ this.storage[this.name + key] = typeof value == 'string' ? value : JSON.stringify( value );
+ }
+}),
+
storage_factory = function( dbname, callback )
{
+ // Use Flash if we have to
// Nothing cool works from file: :(
- if ( LOCAL )
+ if ( LOCAL || ( !indexedDB && !localStorage ) )
{
}
// Try IndexedDB
@@ -113,6 +155,11 @@ storage_factory = function( dbname, callback )
storage_factory( dbname, callback );
};
}
+ // LocalStorage/Fake
+ else
+ {
+ callback( new LocalStorageClass( dbname ) );
+ }
};
return storage_factory;

0 comments on commit 46984aa

Please sign in to comment.