diff --git a/shared/base/view.js b/shared/base/view.js index 2ff8d0c1..ca510daa 100644 --- a/shared/base/view.js +++ b/shared/base/view.js @@ -60,6 +60,10 @@ module.exports = BaseView = Backbone.View.extend({ options.collection_params = options.collection.params; } + if(options.preventPostInitialize){ + this.postInitialize = noop; + } + this.model = options.model; this.collection = options.collection; }, @@ -446,8 +450,11 @@ BaseView.attach = function(app, parentView) { } }); options.app = app; - ViewClass = BaseView.getView(viewName); - view = new ViewClass(options); + view = app.fetcher.viewStore.get(viewName); + if(!view){ + ViewClass = BaseView.getView(viewName); + view = new ViewClass(options); + } view.attach($el, parentView); return view; } diff --git a/shared/fetcher.js b/shared/fetcher.js index 2539dc00..92f87e5c 100644 --- a/shared/fetcher.js +++ b/shared/fetcher.js @@ -33,7 +33,7 @@ and returns an identifying object: } */ -var Backbone, CollectionStore, ModelStore, async, modelUtils, _; +var Backbone, CollectionStore, ModelStore, ViewStore, async, modelUtils, _; _ = require('underscore'); Backbone = require('backbone'); @@ -42,6 +42,7 @@ async = require('async'); modelUtils = require('./modelUtils'); ModelStore = require('./store/model_store'); CollectionStore = require('./store/collection_store'); +ViewStore = require('./store/view_store'); module.exports = Fetcher; @@ -54,6 +55,9 @@ function Fetcher(options) { this.collectionStore = new CollectionStore({ app: this.app }); + this.viewStore = new ViewStore({ + app: this.app + }); } /** diff --git a/shared/handlebarsHelpers.js b/shared/handlebarsHelpers.js index a2bfb89a..68ceba42 100644 --- a/shared/handlebarsHelpers.js +++ b/shared/handlebarsHelpers.js @@ -32,12 +32,26 @@ module.exports = { options.parentView = parentView; } - // get the Backbone.View based on viewName - ViewClass = BaseView.getView(viewName); - view = new ViewClass(options); + // Try to get view stored in app cache + if(app){ + view = app.fetcher.viewStore.get(viewName); + } + + if(!view){ + // get the Backbone.View based on viewName + ViewClass = BaseView.getView(viewName); + view = new ViewClass(options); + if(app){ + app.fetcher.viewStore.set(view); + } + } else { + // re-initialize view with new options + view.initialize(options); + } // create the outerHTML using className, tagName html = view.getHtml(); + return new Handlebars.SafeString(html); }, diff --git a/shared/store/view_store.js b/shared/store/view_store.js new file mode 100644 index 00000000..d18674e3 --- /dev/null +++ b/shared/store/view_store.js @@ -0,0 +1,44 @@ +var MemoryStore, Super, modelUtils, _; + +_ = require('underscore'); +Super = MemoryStore = require('./memory_store'); +modelUtils = require('../modelUtils'); + +module.exports = ViewStore; + +function ViewStore() { + Super.apply(this, arguments); +} + +/** + * Set up inheritance. + */ +ViewStore.prototype = Object.create(Super.prototype); +ViewStore.prototype.constructor = ViewStore; + +ViewStore.prototype.set = function(view) { + var key, viewName; + viewName = modelUtils.modelName(view.constructor); + if (viewName == null) { + throw new Error('Undefined viewName for view'); + } + key = getViewStoreKey(viewName); + return Super.prototype.set.call(this, key, view, null); +}; + +ViewStore.prototype.get = function(viewName) { + var key, view; + key = getViewStoreKey(viewName); + view = Super.prototype.get.call(this, key); + if (view) { + return view; + } +}; + +ViewStore.prototype._formatKey = function(key) { + return Super.prototype._formatKey.call(this, "_vs:" + key); +}; + +function getViewStoreKey(viewName) { + return modelUtils.underscorize(viewName); +}