4 * @class TemplateManager
6 function TemplateManager() {
11 (function () { // strict mode wrapper
13 TemplateManager.prototype = {
21 * UI module initialisation
23 init: function init() {
28 * Returns template html (from cache)
30 get: function TemplateManager_get(tplName, tplParams) {
31 console.log('TemplateManager_get:' + tplName);
33 if (this.cache[tplName] !== undefined) {
34 return this.getCompleted(this.cache[tplName], tplParams);
40 * Load templates to cache
42 loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
48 if ($.isArray(tplNames)) {
51 $.each(tplNames, function (index, fileName) {
53 // cache template html
54 if (self.cache[fileName] === undefined) {
55 tplName = [fileName, app.config.get('templateExtension')].join('');
56 tplPath = [app.config.get('templateDir'), tplName].join('/');
63 success: function (data) {
68 self.cache[fileName] = data;
69 console.log('Cached template: ' + fileName);
71 // if all templates are cached launch callback
72 if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
76 error: function (jqXHR, textStatus, errorThrown) {
81 // template is already cached
83 // if all templates are cached launch callback
84 if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
94 * Returns template completed by specified params
96 getCompleted: function TemplateManager_getCompleted(tplHtml, tplParams) {
97 var tplParam, replaceRegExp;
99 for (tplParam in tplParams) {
100 if (tplParams.hasOwnProperty(tplParam)) {
101 replaceRegExp = new RegExp(['%', tplParam, '%'].join(''), 'g');
102 tplHtml = tplHtml.replace(replaceRegExp, tplParams[tplParam]);