You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			263 lines
		
	
	
		
			7.5 KiB
		
	
	
	
		
			JavaScript
		
	
		
		
			
		
	
	
			263 lines
		
	
	
		
			7.5 KiB
		
	
	
	
		
			JavaScript
		
	
| 
											12 years ago
										 | /** | ||
|  |  * Backbone localStorage Adapter | ||
| 
											11 years ago
										 |  * Version 1.1.14 | ||
| 
											12 years ago
										 |  * | ||
|  |  * https://github.com/jeromegn/Backbone.localStorage
 | ||
|  |  */ | ||
|  | (function (root, factory) { | ||
|  |   if (typeof exports === 'object' && typeof require === 'function') { | ||
|  |     module.exports = factory(require("backbone")); | ||
|  |   } else if (typeof define === "function" && define.amd) { | ||
|  |     // AMD. Register as an anonymous module.
 | ||
|  |     define(["backbone"], function(Backbone) { | ||
|  |       // Use global variables if the locals are undefined.
 | ||
|  |       return factory(Backbone || root.Backbone); | ||
|  |     }); | ||
|  |   } else { | ||
|  |     factory(Backbone); | ||
|  |   } | ||
|  | }(this, function(Backbone) { | ||
|  | // A simple module to replace `Backbone.sync` with *localStorage*-based
 | ||
|  | // persistence. Models are given GUIDS, and saved into a JSON object. Simple
 | ||
|  | // as that.
 | ||
|  | 
 | ||
|  | // Generate four random hex digits.
 | ||
|  | function S4() { | ||
|  |    return (((1+Math.random())*0x10000)|0).toString(16).substring(1); | ||
|  | }; | ||
|  | 
 | ||
|  | // Generate a pseudo-GUID by concatenating random hexadecimal.
 | ||
|  | function guid() { | ||
|  |    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()); | ||
|  | }; | ||
|  | 
 | ||
| 
											11 years ago
										 | function isObject(item) { | ||
|  |   return item === Object(item); | ||
|  | } | ||
|  | 
 | ||
| 
											12 years ago
										 | function contains(array, item) { | ||
|  |   var i = array.length; | ||
|  |   while (i--) if (array[i] === item) return true; | ||
|  |   return false; | ||
|  | } | ||
|  | 
 | ||
|  | function extend(obj, props) { | ||
|  |   for (var key in props) obj[key] = props[key] | ||
|  |   return obj; | ||
|  | } | ||
|  | 
 | ||
| 
											11 years ago
										 | function result(object, property) { | ||
|  |     if (object == null) return void 0; | ||
|  |     var value = object[property]; | ||
|  |     return (typeof value === 'function') ? object[property]() : value; | ||
|  | } | ||
|  | 
 | ||
| 
											12 years ago
										 | // Our Store is represented by a single JS object in *localStorage*. Create it
 | ||
|  | // with a meaningful name, like the name you'd give a table.
 | ||
|  | // window.Store is deprectated, use Backbone.LocalStorage instead
 | ||
|  | Backbone.LocalStorage = window.Store = function(name, serializer) { | ||
|  |   if( !this.localStorage ) { | ||
|  |     throw "Backbone.localStorage: Environment does not support localStorage." | ||
|  |   } | ||
|  |   this.name = name; | ||
|  |   this.serializer = serializer || { | ||
|  |     serialize: function(item) { | ||
| 
											11 years ago
										 |       return isObject(item) ? JSON.stringify(item) : item; | ||
| 
											12 years ago
										 |     }, | ||
|  |     // fix for "illegal access" error on Android when JSON.parse is passed null
 | ||
|  |     deserialize: function (data) { | ||
|  |       return data && JSON.parse(data); | ||
|  |     } | ||
|  |   }; | ||
|  |   var store = this.localStorage().getItem(this.name); | ||
|  |   this.records = (store && store.split(",")) || []; | ||
|  | }; | ||
|  | 
 | ||
|  | extend(Backbone.LocalStorage.prototype, { | ||
|  | 
 | ||
|  |   // Save the current state of the **Store** to *localStorage*.
 | ||
|  |   save: function() { | ||
| 
											11 years ago
										 |     var store = this.localStorage().getItem(this.name); | ||
|  |     this.records = _.union(this.records, store && store.split(",")); | ||
| 
											12 years ago
										 |     this.localStorage().setItem(this.name, this.records.join(",")); | ||
|  |   }, | ||
|  | 
 | ||
|  |   // Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
 | ||
|  |   // have an id of it's own.
 | ||
|  |   create: function(model) { | ||
| 
											11 years ago
										 |     if (!model.id && model.id !== 0) { | ||
| 
											12 years ago
										 |       model.id = guid(); | ||
|  |       model.set(model.idAttribute, model.id); | ||
|  |     } | ||
| 
											11 years ago
										 |     this.localStorage().setItem(this._itemName(model.id), this.serializer.serialize(model)); | ||
| 
											12 years ago
										 |     this.records.push(model.id.toString()); | ||
|  |     this.save(); | ||
| 
											11 years ago
										 |     return this.find(model); | ||
| 
											12 years ago
										 |   }, | ||
|  | 
 | ||
|  |   // Update a model by replacing its copy in `this.data`.
 | ||
|  |   update: function(model) { | ||
| 
											11 years ago
										 |     this.localStorage().setItem(this._itemName(model.id), this.serializer.serialize(model)); | ||
| 
											12 years ago
										 |     var modelId = model.id.toString(); | ||
|  |     if (!contains(this.records, modelId)) { | ||
|  |       this.records.push(modelId); | ||
|  |       this.save(); | ||
|  |     } | ||
| 
											11 years ago
										 |     return this.find(model); | ||
| 
											12 years ago
										 |   }, | ||
|  | 
 | ||
|  |   // Retrieve a model from `this.data` by id.
 | ||
|  |   find: function(model) { | ||
| 
											11 years ago
										 |     var store = this.localStorage().getItem(this.name); | ||
|  |     this.records = (store && store.split(",")) || []; | ||
| 
											11 years ago
										 |     return this.serializer.deserialize(this.localStorage().getItem(this._itemName(model.id))); | ||
| 
											12 years ago
										 |   }, | ||
|  | 
 | ||
|  |   // Return the array of all models currently in storage.
 | ||
|  |   findAll: function() { | ||
| 
											11 years ago
										 |     var store = this.localStorage().getItem(this.name); | ||
|  |     this.records = (store && store.split(",")) || []; | ||
| 
											12 years ago
										 |     var result = []; | ||
|  |     for (var i = 0, id, data; i < this.records.length; i++) { | ||
|  |       id = this.records[i]; | ||
| 
											11 years ago
										 |       data = this.serializer.deserialize(this.localStorage().getItem(this._itemName(id))); | ||
| 
											12 years ago
										 |       if (data != null) result.push(data); | ||
|  |     } | ||
|  |     return result; | ||
|  |   }, | ||
|  | 
 | ||
|  |   // Delete a model from `this.data`, returning it.
 | ||
|  |   destroy: function(model) { | ||
| 
											11 years ago
										 |     this.localStorage().removeItem(this._itemName(model.id)); | ||
| 
											12 years ago
										 |     var modelId = model.id.toString(); | ||
|  |     for (var i = 0, id; i < this.records.length; i++) { | ||
|  |       if (this.records[i] === modelId) { | ||
|  |         this.records.splice(i, 1); | ||
|  |       } | ||
|  |     } | ||
|  |     this.save(); | ||
|  |     return model; | ||
|  |   }, | ||
|  | 
 | ||
|  |   localStorage: function() { | ||
|  |     return localStorage; | ||
|  |   }, | ||
|  | 
 | ||
|  |   // Clear localStorage for specific collection.
 | ||
|  |   _clear: function() { | ||
|  |     var local = this.localStorage(), | ||
|  |       itemRe = new RegExp("^" + this.name + "-"); | ||
|  | 
 | ||
|  |     // Remove id-tracking item (e.g., "foo").
 | ||
|  |     local.removeItem(this.name); | ||
|  | 
 | ||
|  |     // Match all data items (e.g., "foo-ID") and remove.
 | ||
|  |     for (var k in local) { | ||
|  |       if (itemRe.test(k)) { | ||
|  |         local.removeItem(k); | ||
|  |       } | ||
|  |     } | ||
|  | 
 | ||
|  |     this.records.length = 0; | ||
|  |   }, | ||
|  | 
 | ||
|  |   // Size of localStorage.
 | ||
|  |   _storageSize: function() { | ||
|  |     return this.localStorage().length; | ||
| 
											11 years ago
										 |   }, | ||
|  | 
 | ||
|  |   _itemName: function(id) { | ||
|  |     return this.name+"-"+id; | ||
| 
											12 years ago
										 |   } | ||
|  | 
 | ||
|  | }); | ||
|  | 
 | ||
|  | // localSync delegate to the model or collection's
 | ||
|  | // *localStorage* property, which should be an instance of `Store`.
 | ||
|  | // window.Store.sync and Backbone.localSync is deprecated, use Backbone.LocalStorage.sync instead
 | ||
|  | Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(method, model, options) { | ||
| 
											11 years ago
										 |   var store = result(model, 'localStorage') || result(model.collection, 'localStorage'); | ||
| 
											12 years ago
										 | 
 | ||
|  |   var resp, errorMessage; | ||
|  |   //If $ is having Deferred - use it.
 | ||
|  |   var syncDfd = Backbone.$ ? | ||
|  |     (Backbone.$.Deferred && Backbone.$.Deferred()) : | ||
|  |     (Backbone.Deferred && Backbone.Deferred()); | ||
|  | 
 | ||
|  |   try { | ||
|  | 
 | ||
|  |     switch (method) { | ||
|  |       case "read": | ||
|  |         resp = model.id != undefined ? store.find(model) : store.findAll(); | ||
|  |         break; | ||
|  |       case "create": | ||
|  |         resp = store.create(model); | ||
|  |         break; | ||
|  |       case "update": | ||
|  |         resp = store.update(model); | ||
|  |         break; | ||
|  |       case "delete": | ||
|  |         resp = store.destroy(model); | ||
|  |         break; | ||
|  |     } | ||
|  | 
 | ||
|  |   } catch(error) { | ||
|  |     if (error.code === 22 && store._storageSize() === 0) | ||
|  |       errorMessage = "Private browsing is unsupported"; | ||
|  |     else | ||
|  |       errorMessage = error.message; | ||
|  |   } | ||
|  | 
 | ||
|  |   if (resp) { | ||
|  |     if (options && options.success) { | ||
|  |       if (Backbone.VERSION === "0.9.10") { | ||
|  |         options.success(model, resp, options); | ||
|  |       } else { | ||
|  |         options.success(resp); | ||
|  |       } | ||
|  |     } | ||
|  |     if (syncDfd) { | ||
|  |       syncDfd.resolve(resp); | ||
|  |     } | ||
|  | 
 | ||
|  |   } else { | ||
|  |     errorMessage = errorMessage ? errorMessage | ||
|  |                                 : "Record Not Found"; | ||
|  | 
 | ||
|  |     if (options && options.error) | ||
|  |       if (Backbone.VERSION === "0.9.10") { | ||
|  |         options.error(model, errorMessage, options); | ||
|  |       } else { | ||
|  |         options.error(errorMessage); | ||
|  |       } | ||
|  | 
 | ||
|  |     if (syncDfd) | ||
|  |       syncDfd.reject(errorMessage); | ||
|  |   } | ||
|  | 
 | ||
|  |   // add compatibility with $.ajax
 | ||
|  |   // always execute callback for success and error
 | ||
|  |   if (options && options.complete) options.complete(resp); | ||
|  | 
 | ||
|  |   return syncDfd && syncDfd.promise(); | ||
|  | }; | ||
|  | 
 | ||
|  | Backbone.ajaxSync = Backbone.sync; | ||
|  | 
 | ||
|  | Backbone.getSyncMethod = function(model) { | ||
|  |   if(model.localStorage || (model.collection && model.collection.localStorage)) { | ||
|  |     return Backbone.localSync; | ||
|  |   } | ||
|  | 
 | ||
|  |   return Backbone.ajaxSync; | ||
|  | }; | ||
|  | 
 | ||
|  | // Override 'Backbone.sync' to default to localSync,
 | ||
|  | // the original 'Backbone.sync' is still available in 'Backbone.ajaxSync'
 | ||
|  | Backbone.sync = function(method, model, options) { | ||
|  |   return Backbone.getSyncMethod(model).apply(this, [method, model, options]); | ||
|  | }; | ||
|  | 
 | ||
|  | return Backbone.LocalStorage; | ||
|  | })); |