diff options
| author | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-03 14:28:53 -0400 |
|---|---|---|
| committer | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-06 15:27:53 -0400 |
| commit | 03fef46a1e34015cc9fed4baf54fa04285f8798b (patch) | |
| tree | 487e187475fdab29e5f8bc1adbed5b2bf2c842e0 /app | |
| parent | 4a112b88f4f39dfe5ba8df77f12ed5b565665010 (diff) | |
Step towards cleaner resource resolution for views
Diffstat (limited to 'app')
| -rw-r--r-- | app/node_modules/okquery/index.js | 5 | ||||
| -rw-r--r-- | app/node_modules/okview/index.js | 48 |
2 files changed, 34 insertions, 19 deletions
diff --git a/app/node_modules/okquery/index.js b/app/node_modules/okquery/index.js index cfc8d0b..d36feae 100644 --- a/app/node_modules/okquery/index.js +++ b/app/node_modules/okquery/index.js @@ -30,9 +30,8 @@ function createQuery(resource, query) { } function queryDynamic(resource) { - return function(options) { - options = options || {}; - return resource.get(options.id); + return function(id) { + return resource.get(id); } } diff --git a/app/node_modules/okview/index.js b/app/node_modules/okview/index.js index fc92186..5f3c65c 100644 --- a/app/node_modules/okview/index.js +++ b/app/node_modules/okview/index.js @@ -14,17 +14,34 @@ var UNBOUND_ROUTE_PATTERN = /:([a-zA-Z\-_]+)/; function OKView(options) { if (!(this instanceof OKView)) return new OKView(options); options = options || {}; - if (!options.template) throw new Error('No template provided to view.'); - if (!options.meta) throw new Error('No meta resource provided to view'); - if (!options.route) throw new Error('No route provided to view'); + if (!options.template) + throw new Error('No template provided to view.'); + if (!options.meta) + throw new Error('No meta resource provided to view'); + if (!options.route) + throw new Error('No route provided to view'); this.route = options.route; this._template = options.template; - this._meta = options.meta; - this._queries = options.queries || []; + var meta = this._meta = options.meta; + var queries = this._queries = options.queries || []; // Whether this is a view for a specific resource or its // resource will be resolved later - this.unbound = !!UNBOUND_ROUTE_PATTERN.exec(this.route); + // TODO This bound / unbound thing can probably be expressed in a + // less convoluted way. + var unbound = this.unbound = !!UNBOUND_ROUTE_PATTERN.exec(this.route); this._middleware = createMiddleware(this); + this._fetchTemplateData = unbound ? + fetchResourceTemplateData : fetchCollectionTemplateData; + + function fetchResourceTemplateData(id) { + // Bound views only have a single query + // TODO This is super convoluted + return fetchTemplateData(meta, [queries[0].get(id)]); + } + + function fetchCollectionTemplateData() { + return fetchTemplateData(meta, queries); + } } OKView.prototype.middleware = function() { @@ -37,8 +54,8 @@ OKView.prototype.render = function(req, res, data) { }, errorHandler(req, res, data)); }; -OKView.prototype.getTemplateData = function(options) { - return fetchTemplateData(this._meta, this._queries, options); +OKView.prototype.fetchTemplateData = function() { + return this._fetchTemplateData.apply(this, arguments); }; /** @@ -46,9 +63,9 @@ OKView.prototype.getTemplateData = function(options) { */ function createMiddleware(view) { if (view.unbound) { - return collectionMiddleware(view); + return unboundMiddleware(view); } else { - return singleMiddleware(view); + return boundMiddleware(view); } } @@ -60,12 +77,11 @@ function createMiddleware(view) { * Creates middleware for a view which does not * yet have a resource id associated with it */ -function collectionMiddleware(view) { +function unboundMiddleware(view) { var paramName = getParamName(view.route); return function(req, res, next) { - view.getTemplateData({ - id: req.params[paramName] - }).then(function(data) { + var id = req.params[paramName]; + view.fetchTemplateData(id).then(function(data) { view.render(req, res, data); }, errorHandler(req, res, next)); }; @@ -75,9 +91,9 @@ function collectionMiddleware(view) { * Creates middleware for a view which already * has a resource id associated with it */ -function singleMiddleware(view) { +function boundMiddleware(view) { return function(req, res, next) { - view.getTemplateData().then(function(data) { + view.fetchTemplateData().then(function(data) { view.render(req, res, data); }, errorHandler(req, res, next)); }; |
