mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-22 20:52:58 +01:00
Implemented templates feature
This commit is contained in:
41
components/templatesFeature.js
Normal file
41
components/templatesFeature.js
Normal file
@@ -0,0 +1,41 @@
|
||||
(function(app) {
|
||||
|
||||
function Template(html) {
|
||||
this.html = html;
|
||||
}
|
||||
|
||||
Template.prototype.render = function (model) {
|
||||
return app.get('html').element(this.html, model);
|
||||
};
|
||||
|
||||
app.templates = [];
|
||||
app.template = function (key) {
|
||||
var tpl = this.templates[key];
|
||||
if(tpl == null) {
|
||||
throw new Error(key + ' template is not found');
|
||||
}
|
||||
return tpl;
|
||||
}
|
||||
|
||||
app.run(function() {
|
||||
readTemplates(app.rootViewElement);
|
||||
})
|
||||
|
||||
function readTemplates(containerEl) {
|
||||
var els = containerEl.querySelectorAll('[data-template]');
|
||||
var store = app.templates;
|
||||
|
||||
Array.prototype.forEach.call(els, function(element) {
|
||||
var key = element.getAttribute('data-template');
|
||||
|
||||
if(store[key] instanceof Template) {
|
||||
console.warn(key + ' templates already registered');
|
||||
return;
|
||||
}
|
||||
|
||||
store[key] = new Template(element.innerHTML);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
})(window.app);
|
||||
Reference in New Issue
Block a user