Created
June 20, 2013 19:18
-
-
Save anonymous/5825758 to your computer and use it in GitHub Desktop.
local storage data source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// simple setup | |
// localStorage is an object, where the propertyName will be the baseName + _ + recTypeName | |
// the content will be an array, where records can be removed from and push onto | |
SC.LocalStorageDataSource = SC.DataSource.extend({ | |
baseName: null, | |
lsIdFor: function(rectype){ | |
if(!this.get('baseName')) throw new Error("No basename defined in LocalStorageDataSource"); | |
return [this.get('baseName'),"_",rectype.toString()].join(""); | |
}, | |
writeLS: function(id,data){ | |
var d; | |
d = (SC.typeOf(data) !== SC.T_STRING)? JSON.stringify(data): data; | |
localStorage[id] = d; | |
}, | |
readLS: function(id){ | |
return localStorage[id]; | |
}, | |
fetch: function(store,query){ | |
// find the record type from the query | |
var lsId = lsIdFor(query.get('recordType')); | |
var ret = this.readLS(lsId); | |
// push into the store | |
// do something with dataSourceDidCompleteQuery... forgot the exact API | |
}, | |
retrieveRecord: function(store,sk,id){ | |
var recType = store.recordTypeFor(sk); | |
var pk = recType.prototype.primaryKey || 'id'; | |
var lsId = lsIdFor(recType); | |
var allrecs = this.readLS(lsId); | |
var ret = allrecs.findProperty(pk,id); | |
// and push in the store, and return yes | |
}, | |
createRecord: function(store,sk,params){ // no clue what params is | |
var recType = store.recordTypeFor(sk); | |
var pk = recType.prototype.primaryKey || 'id'; | |
var lsId = lsIdFor(recType); | |
var allrecs = this.readLS(lsId) || []; | |
var hash = store.readEditableDataHash(sk); // editable, because we need to assign an id if needed | |
if(!hash[pk]) hash[pk] = allrecs.length; | |
allrecs.push(hash); | |
this.writeLS(lsId,allrecs); | |
// now update the store and return yes | |
}, | |
// and so forth... | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment