Skip to content

Instantly share code, notes, and snippets.

@dmikey
Created December 24, 2015 12:01
Show Gist options
  • Save dmikey/12eb378f079eb6a1e7c1 to your computer and use it in GitHub Desktop.
Save dmikey/12eb378f079eb6a1e7c1 to your computer and use it in GitHub Desktop.
componentize your JS easy.
'use strict';
module.exports = function(template, data) {
this.data = function(data) {
this.oldnode = this.node;
for (var k in data) {
this[k] = data[k];
};
this.tag = this.tag || 'div';
return this;
};
this.update = function() {
this.setup();
this.target.replaceChild(this.node, this.oldnode);
}
this.setup = function() {
if (this.components) {
this.renderComponents();
} else {
this.renderHTML();
this.renderDOM();
}
return this;
};
this.renderHTML = function() {
this.innerHTML = template(this);
return this.innerHTML;
};
this.renderDOM = function() {
this.node = document.createElement(this.tag);
this.node.innerHTML = this.renderHTML();
return this.node;
};
this.renderInto = function(target) {
this.target = target;
target.appendChild(this.renderDOM());
}
this.data(data);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment