Adding in the basics for resoruce view

This commit is contained in:
srkenny@gmail.com
2013-08-09 17:04:36 +01:00
parent 155b78cc1a
commit e6b39628e4
7 changed files with 1960 additions and 0 deletions
+136
View File
@@ -0,0 +1,136 @@
function ResourceManager(options) {
var t = this;
// exports
t.fetchResources = fetchResources;
// locals
var resources = [];
var cache = [];
_addResourceSources(options.resources);
/**
* ----------------------------------------------------------------
* Categorize and add the provided sources
* ----------------------------------------------------------------
*/
function _addResourceSources(sources) {
var resource = {};
if ($.isFunction(sources)) {
// is it a function?
resource = {
resources: sources
};
resources.push(resource);
} else if (typeof sources == 'string') {
// is it a URL string?
resource = {
url: sources
};
resources.push(resource);
} else if (typeof sources == 'object') {
// is it json object?
for (var i=0; i<sources.length; i++) {
var s = sources[i];
normalizeSource(s);
resource = {
resources: s
};
resources.push(resource);
}
}
}
/**
* ----------------------------------------------------------------
* Fetch resources from source array
* ----------------------------------------------------------------
*/
function fetchResources(useCache, currentView) {
// if useCache is not defined, default to true
useCache = useCache || true;
if (useCache) {
// get from cache
return cache;
} else {
// do a fetch resource from source, rebuild cache
cache = [];
var len = resources.length;
for (var i = 0; i < len; i++) {
var resources = _fetchResourceSource(resources[i], currentView);
cache = cache.concat(resources);
}
return cache;
}
}
/**
* ----------------------------------------------------------------
* Fetch resources from each source. If source is a function, call
* the function and return the resource. If source is a URL, get
* the data via synchronized ajax call. If the source is an
* object, return it as is.
* ----------------------------------------------------------------
*/
function _fetchResourceSource(source, currentView) {
var resources = source.resources;
if (resources) {
if ($.isFunction(resources)) {
return resources();
}
} else {
var url = source.url;
if (url) {
var data={};
if (typeof currentView === 'object') {
var startParam = options.startParam;
var endParam = options.endParam;
if (startParam) {
data[startParam] = Math.round(+currentView.visStart / 1000);
}
if (endParam) {
data[endParam] = Math.round(+currentView.visEnd / 1000);
}
}
$.ajax($.extend({}, source, {
data: data,
dataType: 'json',
cache: false,
success: function(res) {
res = res || [];
resources = res;
},
error: function() {
alert("ajax error getting json from "+url);
},
async: false // too much work coordinating callbacks so dumb it down
}));
}
}
return resources;
}
/**
* ----------------------------------------------------------------
* normalize the source object
* ----------------------------------------------------------------
*/
function normalizeSource(source) {
if (source.className) {
if (typeof source.className == 'string') {
source.className = source.className.split(/\s+/);
}
} else {
source.className = [];
}
var normalizers = fc.sourceNormalizers;
for (var i=0; i<normalizers.length; i++) {
normalizers[i](source);
}
}
}