Prototype, prototype render
by amindunited
JavaScript
var Base = function(){};
/**
* Run our data through the template and return an HTML string
* @param {Object} data model attributes
* @return {String} template HTML
*/
Base.prototype.renderTemplate = function(data){
// some rendering code here that uses data
// let's assume magicTemplatingCode returns a string
//return magicTemplatingCode(data);
};
/**
* Set the template string as our .html property
* @param {Object} data model attributes
* @return {Object} base instance
*/
Base.prototype.render = function(data, ext){
console.log("All of your base are rendered ", data, ext);
this.html = this.renderTemplate(data);
this.rendered = true;
return this;
};
// child inherits from base
var Child = function(){};
Child.prototype = Object.create(Base.prototype);
/**
* Do something prior to rendering with our data attributes
* @param {Object} data model attributes
* @return {undefined}
*/
Child.prototype.preRender = function(data){
// we do something here before the template is rendered
// assume this method returns nothing of importance
console.log("Prerendering....");
};
/**
* Extend the Base Object's rendering so that is calls preRender first
* @param {Object} data model attributes
* @return {undefined}
*/
Child.prototype.render = function() {
//Call preRender
this.preRender();
//run this._super();
this.constructor.prototype.render.apply(this, arguments);
}
//Create child
var child = new Child();
//test render with two arguments
child.render("arg_1", "arg_2");