mirror of
https://github.com/rr-/szurubooru.git
synced 2025-07-17 08:26:24 +00:00
Added template loading through AJAX
This commit is contained in:
56
public_html/js/Util.js
Normal file
56
public_html/js/Util.js
Normal file
@ -0,0 +1,56 @@
|
||||
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 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,
|
||||
};
|
||||
});
|
||||
|
||||
App.DI.registerSingleton('util', App.Util);
|
Reference in New Issue
Block a user