2 _App.Template = function()
7 _App.Template.prototype =
14 initialize : function()
20 * Retrieve a template from the server.
25 retrieve : function( type, name, lifespan, callback )
27 if (!this.templateStore[name])
29 this.templateStore[name] = {};
30 this.tempStore[name] = {};
31 this.tempStore[name]["callback"] = callback;
32 var rnd = new Date().getTime();
35 url : 'templates/' + type + '/' + name + '.html?t=' + rnd
36 }, $.proxy( this._retrievedHtml, this ));
39 url : 'templates/' + type + '/' + name + '.js?t=' + rnd
40 }, $.proxy( this._retrievedJs, this ));
44 if (undefined != callback && 'function' == typeof callback)
51 _retrievedJs : function ( name, data)
53 this.tempStore[name]['class'] = "template_" + name;
54 if (this.tempStore[name]['html'])
56 this.templateStore[name] = new window[this.tempStore[name]['class']](
57 this.tempStore[name]['html']
59 if (this.tempStore[name]["callback"])
61 this.tempStore[name]["callback"](name);
63 delete this.tempStore[name];
64 //App.Event.trigger('Template.Retrieved:' + name, {"name": name});
68 _retrievedHtml : function ( name, data)
70 this.tempStore[name]['html'] = data;
71 if (this.tempStore[name]['class'])
73 this.templateStore[name] = new window[this.tempStore[name]['class']](
74 this.tempStore[name]['html']
76 if (this.tempStore[name]["callback"])
78 this.tempStore[name]["callback"](name);
80 delete this.tempStore[name];
81 //App.Event.trigger('Template.Retrieved:' + name, {"name": name});
86 * Register a new template instance.
87 * Template must already have been retrieved from server.
94 register : function( id, type, name, target, data, callback )
96 _t[id] = new this._templateInstance(
102 'Template.Ready:' + name,
103 {"id": name, "pageName": name}
105 if (!this.templateStore[name])
107 this.retrieve(type, name, true, $.proxy( this._hydrateTemplate, this, callback, id ));
111 _t[id].setTemplate(this.templateStore[name]);
114 this.instanceCounter++;
118 _hydrateTemplate : function ( callback, id, name )
120 _t[id].setTemplate(this.templateStore[name]);
124 _templateInstance : function( tid, template, target, data )
127 this.template = template;
128 this.target = target;
130 this.published = false;
131 this.autoPublish = [];
132 if (this.template && this.template.static)
134 this.construct = this.template.static.replaceAll('[tid]', this.tid);
135 if (undefined != this.template.init)
137 this.template.init(this);
142 * Do something when we publish.
144 this.onPublish = function ( callback )
148 this.autoPublish.push(callback);
157 * Set the template to work with for this instance.
160 this.setTemplate = function ( template )
162 this.template = template;
163 this.construct = this.template.static
164 ? this.template.static.replaceAll('[tid]', this.tid)
166 if (undefined != this.template.init)
168 this.template.init(this);
172 this.hydrate(this.data);
177 * Hydrate template with dataset.
178 * This can be called before and after template publication.
181 this.hydrate = function( data )
186 for (var element in this.template.elements)
188 var elem = this.template.elements[element];
189 value = (data[element])
194 elem.hydrateLive(this.tid, value);
198 this.construct = elem.hydrate(this.construct, value);
203 $('.selectpicker').selectpicker();
207 if (this.template && this.data)
209 this.hydrate(this.data);
212 this.hydratePartial = function( data )
218 if (this.template && this.template.elements)
220 for (var element in data)
222 if (this.template.elements[element])
224 var elem = this.template.elements[element];
225 value = (data[element])
228 this.data[element] = value;
231 elem.hydrateLive(this.tid, value);
235 this.construct = elem.hydrate(this.construct, value);
241 $('.selectpicker').selectpicker();
247 * Hydrate template with a single parameter.
248 * This can be called before and after template publication.
252 this.hydrateParam = function( param, value )
258 this.data[param] = value;
259 if (this.template && this.template.elements && this.template.elements[param])
261 var elem = this.template.elements[param];
264 elem.hydrateLive(this.tid, value);
265 $('.selectpicker').selectpicker();
269 this.construct = elem.hydrate(this.construct, value);
275 * Publish template to registered target.
277 this.publish = function()
279 $('#' + this.target).html(
280 '<div id="' + this.tid + '">' + this.construct + '</div>'
282 this.published = true;
283 $('.selectpicker').selectpicker();
284 for (var element in this.template.elements)
286 this.template.elements[element].publish(this.tid);
288 if (this.template.construct)
290 this.template.construct();
292 for (var i in this.autoPublish)
294 this.autoPublish[i]();
296 this.autoPublish = [];
300 * Harvest dataset from published template.
301 * @returns {___anonymous3461_3462}
303 this.harvest = function()
306 for (var element in this.template.elements)
308 newData[element] = this.template.elements[element].harvest(this.tid);
314 * Unpublish template.
315 * Template can be re-published afterward.
317 this.remove = function()
321 this.template.destruct();
323 $('#' + this.tid).remove();
324 this.published = false;