-
Notifications
You must be signed in to change notification settings - Fork 311
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm I think the |
||
app: this.app | ||
}); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to use |
||
} | ||
|
||
// create the outerHTML using className, tagName | ||
html = view.getHtml(); | ||
|
||
return new Handlebars.SafeString(html); | ||
}, | ||
|
||
|
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); | ||
} |
There was a problem hiding this comment.
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?