Skip to content

Instantly share code, notes, and snippets.

Created February 21, 2013 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5003727 to your computer and use it in GitHub Desktop.
Save anonymous/5003727 to your computer and use it in GitHub Desktop.
define([
'dojo/store/JsonRest',
'dojo/store/Memory',
'dojo/store/Observable',
'dojo/_base/lang',
'dojo/_base/array',
'dojo/request',
'dojo/aspect',
'../xhrerror'
], function(
JsonRest, Memory, Observable, lang, array, request, aspect, xhrerror
) {
var refreshTimeout = 1250;
var storeList = {};
var timerList = [];
/**
* Grid store wrapper class. Uses a JSON REST store for getting and
* setting data. Caches all data locally in a Memory store.
*
* Receives update diffs from server and applies to Memory store.
* Write actions are passed on to server.
*
* Keeps record of created stores and returns the already
* created instance if present.
*
* @class apiStore
* @module Common
* @static
* */
/**
* @method apiStore
* @param target {String} API Target.
* @return {Store} Store object.
* */
var createApiStore = function (target) {
var apiStore, cacheStore, dataStore, pollTimestamp = 0;
// don't create multiple instances of the same store
if (storeList.hasOwnProperty(target)) {
return storeList[target];
}
cacheStore = new Observable(new Memory());
dataStore = new JsonRest({ target: target });
// create a store object for the grid
// get and query uses local store
// all other actions use data store
apiStore = lang.delegate(cacheStore, {
add: lang.hitch(dataStore, dataStore.add),
put: lang.hitch(dataStore, dataStore.put),
remove: lang.hitch(dataStore, dataStore.remove)
});
// poll for changes to dataset
function poll(arg) {
request.get(
target + 'diff/' + pollTimestamp,
{ handleAs: 'json' }
).then(function (data) {
pollTimestamp = data.timestamp;
array.forEach(data.updated, function (element) {
cacheStore.put(element);
});
array.forEach(data.deleted, function (element) {
cacheStore.remove(element);
});
}, xhrerror);
// pass on return value (for aspect.after)
return arg;
}
// poll() before populating store with initial data to
// get initial timestamp
poll();
// populate local store with all data
// add Deferred to apiStore to notify Grid when store is ready
apiStore.ready = dataStore.query({}).forEach(function (element) {
cacheStore.put(element);
});
aspect.after(apiStore, 'add', poll);
aspect.after(apiStore, 'put', poll);
aspect.after(apiStore, 'remove', poll);
timerList.push(setInterval(poll, refreshTimeout));
storeList[target] = apiStore;
return apiStore;
};
/**
* Clears all saved and active stores
* @method apiStore.clearStores
* */
createApiStore.clearStores = function () {
var i;
for (i = 0; i < timerList.length; i += 1) {
clearInterval(timerList[i]);
}
timerList = [];
storeList = {};
};
return createApiStore;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment