Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save and re-use views from memory store #78

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions shared/base/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ module.exports = BaseView = Backbone.View.extend({
options.collection_params = options.collection.params;
}

if(options.preventPostInitialize){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We wouldn't need preventPostInitialize anymore, right?

this.postInitialize = noop;
}

this.model = options.model;
this.collection = options.collection;
},
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion shared/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;

Expand All @@ -54,6 +55,9 @@ function Fetcher(options) {
this.collectionStore = new CollectionStore({
app: this.app
});
this.viewStore = new ViewStore({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I think the viewStore would better live on the app, instead of app.fetcher, because it doesn't deal with fetching data.

app: this.app
});
}

/**
Expand Down
20 changes: 17 additions & 3 deletions shared/handlebarsHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to use view.parseOptions(options) to prevent unintended side effects.

}

// create the outerHTML using className, tagName
html = view.getHtml();

return new Handlebars.SafeString(html);
},

Expand Down
44 changes: 44 additions & 0 deletions shared/store/view_store.js
Original file line number Diff line number Diff line change
@@ -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);
}