[Example] Bring in example persistence

Bring in BrowserPersistenceService as an example, to use
as a stub to develop and integrate commonUI bundles
against. WTD-574.
This commit is contained in:
Victor Woeltjen 2014-11-23 17:58:19 -08:00
parent 1b0303e517
commit 8e50078823
2 changed files with 100 additions and 0 deletions

View File

@ -0,0 +1,18 @@
{
"extensions": {
"components": [
{
"provides": "persistenceService",
"type": "provider",
"implementation": "BrowserPersistenceProvider.js",
"depends": [ "$q", "PERSISTENCE_SPACE" ]
}
],
"constants": [
{
"key": "PERSISTENCE_SPACE",
"value": "mct"
}
]
}
}

View File

@ -0,0 +1,82 @@
/*global define*/
/**
* Stubbed implementation of a persistence provider,
* to permit objects to be created, saved, etc.
*/
define(
[],
function () {
'use strict';
function BrowserPersistenceProvider($q, SPACE) {
var spaces = SPACE ? [SPACE] : [],
caches = {},
promises = {
as: function (value) {
return $q.when(value);
}
};
spaces.forEach(function (space) {
caches[space] = {};
});
return {
listSpaces: function () {
return promises.as(spaces);
},
listObjects: function (space) {
var cache = caches[space];
return promises.as(
cache ? Object.keys(cache) : null
);
},
createObject: function (space, key, value) {
var cache = caches[space];
if (!cache || cache[key]) {
return promises.as(null);
}
cache[key] = value;
return promises.as(true);
},
readObject: function (space, key) {
var cache = caches[space];
return promises.as(
cache ? cache[key] : null
);
},
updateObject: function (space, key, value) {
var cache = caches[space];
if (!cache || !cache[key]) {
return promises.as(null);
}
cache[key] = value;
return promises.as(true);
},
deleteObject: function (space, key, value) {
var cache = caches[space];
if (!cache || !cache[key]) {
return promises.as(null);
}
delete cache[key];
return promises.as(true);
}
};
}
return BrowserPersistenceProvider;
}
);