Created
December 24, 2015 12:01
-
-
Save dmikey/12eb378f079eb6a1e7c1 to your computer and use it in GitHub Desktop.
componentize your JS easy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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