All Downloads are FREE. Search and download functionalities are using the official Maven repository.

js.mv.Renderer.js Maven / Gradle / Ivy

The newest version!
define(function(require){

	var $ = require("jquery"),
		Handlebars = require("handlebars"),
		ko = require("knockout");

	/**
	 * Renders a page recursively
	 */
	var renderRecursive = function(model, el){
		if (typeof(model.html) === "string"){
			//String view, create template
			var tmpl = Handlebars.compile(model.html);
			//Apply template with current model
			el.html( tmpl(model) );
		} else if (typeof(model.html) === "function"){
			//function template
			el.html( model.html(model) );
		}

		//Try to unbind model
		ko.cleanNode(el[0]);
		
		//Apply binding to immediate model
		ko.applyBindings(model, el[0]);
		
		//Invoke after render trigger
		if (typeof(model.afterRender) === "function")
			model.afterRender(el);

		//Recurse with data-model attribute
		el.find("*").each(function(i, el){
			var el = $(el),
				subModel = el.data("model");
			
			if (subModel){
				//attribute in dom found
				if (model[subModel]){
					//property in model found
					renderRecursive(model[subModel], el);
				} else
					throw "Submodel: " + subModel + " not found in model: " + model;
			}
		});
	};

	return renderRecursive;
		
});




© 2015 - 2025 Weber Informatics LLC | Privacy Policy