Browse files

view-renderer is now a class and a factory to avoid creating renderer…

… objects over and over again. removing viewId argument that is not really relevant for this abstraction.
  • Loading branch information...
1 parent 9a40739 commit da53ea37b3f497b312b5dfd6166faa431485ffbb @caridy committed Oct 30, 2012
Showing with 18 additions and 6 deletions.
  1. +18 −6 lib/app/autoload/view-renderer.common.js
View
24 lib/app/autoload/view-renderer.common.js
@@ -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];
}
@@ -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;

0 comments on commit da53ea3

Please sign in to comment.