/** * Main file to load all client-side data caches */ cambrient.client_data = {}; cambrient.client_data.Main = function() { var self; function constructorFn() { self = this; } // load up all the client data scripts. Provide an array of client data names and a path (excluding contextPath), e.g. // cambrient.client_data.initClientData('/shared/templates/js/modules/client_data', ['user_details', 'schemes']); constructorFn.prototype.initClientData = function(path, arrayClientDataNames) { arrayClientDataNames.each(function(scriptName) { // Load the plugin script asynchronously loadScript(path + scriptName + ".js"); }); } // initialize the client data store module with queuing and callback chaining constructorFn.prototype.clearCache = function(){ clientDataNames.each(function(cdName) { cambrient.client_data[cdName].clearCache(); }); } // initialize the client data store module with queuing and callback chaining constructorFn.prototype.init = function(cd) { if (cd) { cd.queue = {}; // buffer for requests to this client data cd.cache = {}; // cache so that requests are not repeated cd.callbacks = {}; // buffer of callbacks for thie client data // function to clear the cached data cd.clearCache = function(url, args) { if (!url) { cd.cache = {}; return; } if (!args) args = []; var ajaxUrl = url + '?'; args.each(function(arg){ url += '&' + arg; }); if (cd.cache[ajaxUrl] != null) { cd.cache[ajaxUrl] == null; } } cd.logData = function(data) { alert('text'); } // function to handle getting data from the server with callback chaining and request queueing cd.getDataFromServer = function(url, args, callBack) { var ajaxUrl = url; if (args.length > 0) url += '?'; args.each(function(arg){ url += '&' + arg; }); if (cd.callbacks[ajaxUrl] == null) cd.callbacks[ajaxUrl] = []; if (isFunction(callBack)) cd.callbacks[ajaxUrl].push(callBack); if (!args) args = []; if (cd.cache[ajaxUrl] != null) { // we have a cache of this data cd.callbacks[ajaxUrl].each(function(callBack) { callBack(cd.cache[ajaxUrl]); }); cd.callbacks[ajaxUrl] = []; return; } // check queue to see if current params are being processed to avoid duplicate calls if (cd.queue[ajaxUrl] != null) { // this request is being processed return; } cd.queue[ajaxUrl] = true; cambrient.ajax.request( ajaxUrl, function(reply) { try { // clear queue cd.queue[ajaxUrl] = null; var data = cambrient.getData(reply); cd.cache[ajaxUrl] = data; // call back each interested party var actions = cambrient.getData(reply); if (data) cd.callbacks[ajaxUrl].each(function(callBack) { callBack(data); }); cd.callbacks[ajaxUrl] = []; } catch (e) { cambrient.userMessaging.error(e); } } ); } } } return new constructorFn(); } cambrient.client_data.main = new cambrient.client_data.Main();