mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-23 13:12:42 +01:00
React boilerplate
This commit is contained in:
51
src.old/js/components/templatesFeature.js
Normal file
51
src.old/js/components/templatesFeature.js
Normal file
@@ -0,0 +1,51 @@
|
||||
(function(app) {
|
||||
"use strict";
|
||||
|
||||
function Template(html, isCompiled) {
|
||||
this.html = html;
|
||||
this.isCompiled = isCompiled === true;
|
||||
}
|
||||
|
||||
Template.prototype.render = function (model) {
|
||||
if(this.isCompiled) {
|
||||
return app.get('html').element(this.process(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.get('rootView'));
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
var template = new Template(element.innerHTML);
|
||||
store[key] = template;
|
||||
|
||||
if (element.hasAttribute('data-compiled')) {
|
||||
template.process = app.get('html').compileTemplate(template.html);
|
||||
template.isCompiled = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
})(window.app);
|
||||
Reference in New Issue
Block a user