szurubooru/public_html/js/Util.js

69 lines
1.6 KiB
JavaScript
Raw Normal View History

2014-09-02 09:36:42 +02:00
var App = App || {};
App.Util = (function(jQuery) {
var templateCache = {};
function loadTemplate(templateName) {
return loadTemplateFromCache(templateName)
|| loadTemplateFromDOM(templateName)
|| loadTemplateWithAJAX(templateName);
}
function loadTemplateFromCache(templateName) {
if (templateName in templateCache) {
return new Promise(function(resolve, reject) {
resolve(templateCache[templateName]);
});
}
}
function loadTemplateFromDOM(templateName) {
var $template = jQuery('#' + templateName + '-template');
if ($template.length) {
return new Promise(function(resolve, reject) {
resolve($template.html());
});
}
return null;
}
function initPresenter(presenterGetter, args) {
var presenter = presenterGetter();
presenter.init.call(presenter, args);
}
function initContentPresenter(presenterGetter, args) {
//jQuery('#content').empty();
initPresenter(presenterGetter, args);
};
2014-09-02 09:36:42 +02:00
function loadTemplateWithAJAX(templateName) {
return new Promise(function(resolve, reject) {
var templatesDir = '/templates';
var templateUrl = templatesDir + '/' + templateName + '.tpl';
var templateString;
$.ajax({
url: templateUrl,
method: 'GET',
success: function(data, textStatus, xhr) {
resolve(data);
},
error: function(xhr, textStatus, errorThrown) {
console.log(Error('Error while loading template ' + templateName + ': ' + errorThrown));
reject();
},
});
});
}
return {
loadTemplate: loadTemplate,
initPresenter : initPresenter,
initContentPresenter: initContentPresenter,
2014-09-02 09:36:42 +02:00
};
});
App.DI.registerSingleton('util', App.Util);