From 7359b6df01409709e18db7e2a2cbb5c04bb0ec69 Mon Sep 17 00:00:00 2001 From: eleventigers Date: Thu, 20 Jun 2013 14:43:26 +0100 Subject: [PATCH 1/3] remove view to prevent duplicates --- shared/handlebarsHelpers.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/shared/handlebarsHelpers.js b/shared/handlebarsHelpers.js index a2bfb89a..60b9407e 100644 --- a/shared/handlebarsHelpers.js +++ b/shared/handlebarsHelpers.js @@ -38,6 +38,12 @@ module.exports = { // create the outerHTML using className, tagName html = view.getHtml(); + + // remove view as its parentView will re-create it from data-view attributes present in html + if (typeof window !== 'undefined') { + view.remove(); + } + return new Handlebars.SafeString(html); }, From 62c9ee767c12bcc2b3b389d45f08b3224cba4eb3 Mon Sep 17 00:00:00 2001 From: eleventigers Date: Thu, 20 Jun 2013 21:39:04 +0100 Subject: [PATCH 2/3] prevent postInitialize on views from helpers --- shared/base/view.js | 4 ++++ shared/handlebarsHelpers.js | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/shared/base/view.js b/shared/base/view.js index 2ff8d0c1..0c026a2c 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; }, diff --git a/shared/handlebarsHelpers.js b/shared/handlebarsHelpers.js index 60b9407e..ef99345a 100644 --- a/shared/handlebarsHelpers.js +++ b/shared/handlebarsHelpers.js @@ -32,6 +32,9 @@ module.exports = { options.parentView = parentView; } + // Tell view not to call postInitialize + options.preventPostInitialize = true; + // get the Backbone.View based on viewName ViewClass = BaseView.getView(viewName); view = new ViewClass(options); @@ -40,8 +43,8 @@ module.exports = { html = view.getHtml(); // remove view as its parentView will re-create it from data-view attributes present in html - if (typeof window !== 'undefined') { - view.remove(); + if (!global.isServer) { + view.remove(); } return new Handlebars.SafeString(html); From be9c13e34a2b9685c771be1eed5a5f6733325982 Mon Sep 17 00:00:00 2001 From: eleventigers Date: Fri, 21 Jun 2013 12:30:17 +0100 Subject: [PATCH 3/3] added view_store to reuse views --- shared/base/view.js | 7 ++++-- shared/fetcher.js | 6 ++++- shared/handlebarsHelpers.js | 25 ++++++++++++--------- shared/store/view_store.js | 44 +++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 shared/store/view_store.js diff --git a/shared/base/view.js b/shared/base/view.js index 0c026a2c..ca510daa 100644 --- a/shared/base/view.js +++ b/shared/base/view.js @@ -450,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 ef99345a..68ceba42 100644 --- a/shared/handlebarsHelpers.js +++ b/shared/handlebarsHelpers.js @@ -32,21 +32,26 @@ module.exports = { options.parentView = parentView; } - // Tell view not to call postInitialize - options.preventPostInitialize = true; + // Try to get view stored in app cache + if(app){ + view = app.fetcher.viewStore.get(viewName); + } - // get the Backbone.View based on viewName - ViewClass = BaseView.getView(viewName); - view = new ViewClass(options); + 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(); - // remove view as its parentView will re-create it from data-view attributes present in html - if (!global.isServer) { - view.remove(); - } - 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); +}