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;
|
|
|
|
}
|
|
|
|
|
2014-09-03 09:10:26 +02:00
|
|
|
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,
|
2014-09-03 09:10:26 +02:00
|
|
|
initPresenter : initPresenter,
|
|
|
|
initContentPresenter: initContentPresenter,
|
2014-09-02 09:36:42 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
App.DI.registerSingleton('util', App.Util);
|