Skip to content

Commit

Permalink
view-renderer is now a class and a factory to avoid creating renderer…
Browse files Browse the repository at this point in the history
… objects over and over again. removing viewId argument that is not really relevant for this abstraction.
  • Loading branch information
caridy committed Oct 30, 2012
1 parent 9a40739 commit da53ea3
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/app/autoload/view-renderer.common.js
Expand Up @@ -11,19 +11,30 @@

YUI.add('mojito-view-renderer', function(Y) {


var cache = {};


/*
* Mojito's view renderer abstraction. Will plugin in the specified view
* plugin to do the rendering, depending on the 'type' specified.
* plugin to do the rendering, depending on the 'type' specified. This Class
* can also be used as a factory to avoid creating renderer objects, and to
* do so, you just need to call `Y.mojito.ViewRenderer('hb', {});` and it will
* return the cached engine instead of creating a facade for it.
* @class ViewRenderer
* @namespace Y.mojit
* @namespace Y.mojito
* @constructor
* @param {String} type view engine addon type to use
* @param {String} viewId
* @param {Object} options
* @param {Object} options View engines configuration.
*/
function Renderer(type, viewId, options) {
function Renderer(type, options) {
type = type || 'hb';
this._renderer = new (Y.mojito.addons.viewEngines[type])(viewId, options);
if (!cache[type]) {
this._renderer = cache[type] = new (Y.mojito.addons.viewEngines[type])(options);
} else {
this._renderer = cache[type];
}
return cache[type];
}


Expand All @@ -44,6 +55,7 @@ YUI.add('mojito-view-renderer', function(Y) {
render: function(data, mojitType, tmpl, adapter, meta, more) {
this._renderer.render(data, mojitType, tmpl, adapter, meta, more);
}

};

Y.namespace('mojito').ViewRenderer = Renderer;
Expand Down

0 comments on commit da53ea3

Please sign in to comment.