fix(files) multiple loading of config.json

This commit is contained in:
coderaiser 2014-06-18 09:01:09 -04:00
parent fd5a89c450
commit 0cd1b17266

View file

@ -9,9 +9,10 @@ var Util, DOM;
DOMProto.Files = new FilesProto(Util, DOM);
function FilesProto(Util, DOM) {
var Files = this,
var Promises = {},
Files = this,
FILES_JSON = 'config|modules|ext',
FILES_HTML = 'file|path|link|pathLink',
FILES_HTML = 'config-tmpl|file|path|link|pathLink',
funcs = [],
Data = {},
DIR_HTML = '/html/',
@ -68,20 +69,33 @@ var Util, DOM;
isHTML = name.match(regExpHTML),
isJSON = name.match(regExpJSON);
if (!isHTML && !isJSON)
if (!isHTML && !isJSON) {
showError(name);
else
if (isHTML) {
path = DIR_HTML_FS + name + '.html',
getSystemFile(Data[name], path , callback);
} else if (isJSON) {
if (name === 'config') {
getConfig(callback);
} else {
path = DIR_JSON + name + '.json';
getSystemFile(Data[name], path, callback);
}
}
} else if (name === 'config') {
getConfig(callback);
} else {
path = getPath(name, isHTML, isJSON);
getSystemFile(path , callback);
}
}
function getPath(name, isHTML, isJSON) {
var path;
if (isHTML) {
if (name === 'config-tmpl')
path = DIR_HTML + Util.rmStr(name, '-tmpl');
else
path = DIR_HTML_FS + name;
path += '.html';
} else if (isJSON) {
path = DIR_JSON + name + '.json';
}
return path;
}
function showError(name) {
@ -91,33 +105,39 @@ var Util, DOM;
throw(error);
}
function getSystemFile(global, url, callback) {
var success = Util.exec.with(callback, null);
Util.exec.if(global, success, function() {
DOM.load.ajax({
url : url,
success : function(local) {
global = local;
success(local);
},
error : function(error) {
callback(error);
}
function getSystemFile(url, callback) {
if (!Promises[url])
Promises[url] = new Promise(function(resolve, reject) {
DOM.load.ajax({
url : url,
success : resolve,
error : reject
});
});
Promises[url].then(function(data) {
if (!Data[url])
Data[url] = data;
callback(null, data);
}, function(error) {
callback(error);
});
}
function getConfig(callback) {
var func = Util.exec.with(callback, null);
var RESTful = DOM.RESTful;
Util.exec.if(Data.config, func, function(callback) {
var RESTful = DOM.RESTful;
RESTful.Config.read(function(config) {
Data.config = config;
callback(config);
if (!Promises.config)
Promises.config = new Promise(function(resolve) {
RESTful.Config.read(resolve);
});
Promises.config.then(function(data) {
if (!Data.config)
Data.config = data;
callback(null, data);
});
}
}