(function(root,factory){if(typeof define==="function"&&define.amd){define(["underscore","backbone"],function(_,Backbone){return factory(_||root._,Backbone||root.Backbone)})}else{factory(_,Backbone)}})(this,function(_,Backbone){function S4(){return((1+Math.random())*65536|0).toString(16).substring(1)}function guid(){return S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()}Backbone.LocalStorage=window.Store=function(name){this.name=name;var store=this.localStorage().getItem(this.name);this.records=store&&store.split(",")||[]};_.extend(Backbone.LocalStorage.prototype,{save:function(){this.localStorage().setItem(this.name,this.records.join(","))},create:function(model){if(!model.id){model.id=guid();model.set(model.idAttribute,model.id)}this.localStorage().setItem(this.name+"-"+model.id,JSON.stringify(model));this.records.push(model.id.toString());this.save();return this.find(model)},update:function(model){this.localStorage().setItem(this.name+"-"+model.id,JSON.stringify(model));if(!_.include(this.records,model.id.toString()))this.records.push(model.id.toString());this.save();return this.find(model)},find:function(model){return this.jsonData(this.localStorage().getItem(this.name+"-"+model.id))},findAll:function(){return _(this.records).chain().map(function(id){return this.jsonData(this.localStorage().getItem(this.name+"-"+id))},this).compact().value()},destroy:function(model){if(model.isNew())return false;this.localStorage().removeItem(this.name+"-"+model.id);this.records=_.reject(this.records,function(id){return id===model.id.toString()});this.save();return model},localStorage:function(){return localStorage},jsonData:function(data){return data&&JSON.parse(data)}});Backbone.LocalStorage.sync=window.Store.sync=Backbone.localSync=function(method,model,options){var store=model.localStorage||model.collection.localStorage;var resp,errorMessage,syncDfd=$.Deferred&&$.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===DOMException.QUOTA_EXCEEDED_ERR&&window.localStorage.length===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)}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};Backbone.sync=function(method,model,options){return Backbone.getSyncMethod(model).apply(this,[method,model,options])};return Backbone.LocalStorage});