connect-traversal
Advanced tools
Comparing version 0.3.2 to 0.3.3
186
lib/chain.js
@@ -8,19 +8,25 @@ 'use strict'; | ||
function Chain() { | ||
this._method = this.ignore; | ||
this._name = this.ignore; | ||
this._parent = this.ignore; | ||
this._order = 0; | ||
var self = this; | ||
self._options = { | ||
method: 'GET', | ||
name: 'index', | ||
parent: self._ignore, | ||
xhr: self._ignore | ||
}; | ||
self._order = 0; | ||
} | ||
Chain.prototype.ignore = '*'; | ||
Chain.prototype.subscribeAttr = 'subscribe'; | ||
Chain.prototype.viewAttr = 'view'; | ||
Chain.prototype._ignore = '*'; | ||
Chain.prototype._subscribeAttr = 'subscribe'; | ||
Chain.prototype._viewAttr = 'view'; | ||
Chain.prototype._optionsName = ['parent', 'name', 'method', 'xhr']; | ||
/** | ||
* Method setter | ||
* @param method | ||
* Parent setter | ||
* @param value | ||
* @returns {*} | ||
*/ | ||
Chain.prototype.method = function(method) { | ||
this._method = method ? method.toLowerCase() : this.ignore; | ||
Chain.prototype.parent = function(value) { | ||
this._options.parent = value || this._ignore; | ||
return this; | ||
@@ -31,7 +37,7 @@ }; | ||
* Name setter | ||
* @param name | ||
* @param value | ||
* @returns {*} | ||
*/ | ||
Chain.prototype.name = function(name) { | ||
this._name = name || this.ignore; | ||
Chain.prototype.name = function(value) { | ||
this._options.name = value || ''; | ||
return this; | ||
@@ -41,8 +47,8 @@ }; | ||
/** | ||
* Parent setter | ||
* @param parent | ||
* Method setter | ||
* @param value | ||
* @returns {*} | ||
*/ | ||
Chain.prototype.parent = function(parent) { | ||
this._parent = parent || this.ignore; | ||
Chain.prototype.method = function(value) { | ||
this._options.method = value ? value.toUpperCase() : 'GET'; | ||
return this; | ||
@@ -52,2 +58,12 @@ }; | ||
/** | ||
* XHR setter | ||
* @param value | ||
* @returns {*} | ||
*/ | ||
Chain.prototype.xhr = function(value) { | ||
this._options.xhr = typeof value == 'boolean' ? value : this._ignore; | ||
return this; | ||
}; | ||
/** | ||
* Options setter | ||
@@ -58,6 +74,7 @@ * @param options | ||
Chain.prototype.options = function(options) { | ||
this.name(options.name); | ||
this.parent(options.parent); | ||
this.method(options.method); | ||
return this; | ||
var self = this; | ||
self._optionsName.forEach(function(name) { | ||
self[name](options[name]); | ||
}); | ||
return self; | ||
}; | ||
@@ -67,52 +84,14 @@ | ||
* Returns sorted list of callbacks by name, parent, method. | ||
* @param name | ||
* @param parent | ||
* @param method | ||
* @param options | ||
* @returns {*} | ||
*/ | ||
Chain.prototype.getCallbacks = function(name, parent, method) { | ||
name = name || this.ignore; | ||
parent = parent || this.ignore; | ||
method = method || this.ignore; | ||
Chain.prototype.getCallbacks = function(options) { | ||
var callbacks = {}, | ||
result = [], | ||
self = this; | ||
var only = self.__getCallbacks(this.viewAttr, name, parent, method, this.ignore); | ||
if (!only) return []; | ||
callbacks[only.order] = only.callbacks; | ||
// get cases for subscribe callbacks. | ||
var cases = [ | ||
[this.subscribeAttr, this.ignore, this.ignore, this.ignore] | ||
]; | ||
if (method !== this.ignore) { | ||
cases.push([this.subscribeAttr, this.ignore, this.ignore, method]); | ||
if (parent !== this.ignore) { | ||
cases.push([this.subscribeAttr, this.ignore, parent, method]); | ||
if (name !== this.ignore) { | ||
cases.push([this.subscribeAttr, name, parent, method]); | ||
} | ||
} else { | ||
if (name !== this.ignore) { | ||
cases.push([this.subscribeAttr, name, this.ignore, method]); | ||
} | ||
} | ||
} | ||
if (parent !== this.ignore) { | ||
cases.push([this.subscribeAttr, this.ignore, parent, this.ignore]); | ||
if (name !== this.ignore) { | ||
cases.push([this.subscribeAttr, name, parent, this.ignore]); | ||
} | ||
} | ||
if (name !== this.ignore) { | ||
cases.push([this.subscribeAttr, name, this.ignore, this.ignore]); | ||
} | ||
cases.forEach(function(params) { | ||
var fns = self.__getCallbacks.apply(self, params); | ||
if (fns) { | ||
callbacks[fns.order] = fns.callbacks; | ||
} | ||
var callbacksObjects = self.__getCallbacksObjects(self._viewAttr, options); | ||
if (!callbacksObjects.length) return []; | ||
callbacksObjects = callbacksObjects.concat(self.__getCallbacksObjects(self._subscribeAttr, options, true)); | ||
callbacksObjects.forEach(function(obj) { | ||
callbacks[obj.order] = obj.callbacks; | ||
}); | ||
@@ -122,2 +101,3 @@ Object.keys(callbacks).sort().forEach(function(key){ | ||
}); | ||
return result; | ||
@@ -132,3 +112,3 @@ }; | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
args.unshift(this.subscribeAttr); | ||
args.unshift(this._subscribeAttr); | ||
return this.__reg.apply(this, args); | ||
@@ -143,3 +123,3 @@ }; | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
args.unshift(this.viewAttr); | ||
args.unshift(this._viewAttr); | ||
return this.__reg.apply(this, args); | ||
@@ -150,2 +130,3 @@ }; | ||
var callbacks = Array.prototype.slice.call(arguments, 1); | ||
var self = this; | ||
if (!callbacks.length) { | ||
@@ -158,25 +139,56 @@ throw TypeError('required at least 1 callback'); | ||
}); | ||
if (!this['_' + type]) this['_' + type] = {}; | ||
var typeObj = this['_' + type]; | ||
if (!typeObj[this._name]) typeObj[this._name] = {}; | ||
if (!typeObj[this._name][this._parent]) typeObj[this._name][this._parent] = {}; | ||
typeObj[this._name][this._parent][this._method] = { | ||
order: this._order++, | ||
callbacks: callbacks | ||
}; | ||
if (!self['_' + type]) self['_' + type] = {}; | ||
var obj = self['_' + type]; | ||
self._optionsName.forEach(function(name, i) { | ||
var option = self._options[name]; | ||
if (!obj[option]) { | ||
if (i == self._optionsName.length - 1) { | ||
obj[option] = { | ||
order: self._order++, | ||
callbacks: callbacks | ||
}; | ||
} else { | ||
obj[option] = {}; | ||
} | ||
} | ||
obj = obj[option]; | ||
}); | ||
return this; | ||
}; | ||
Chain.prototype.__getCallbacks = function(type, name, parent, method, def) { | ||
if (this['_' + type]) { | ||
if (name = this['_' + type][name || def]) { | ||
parent = name[parent || def] || name[def]; | ||
if (parent) { | ||
return parent[method.toLowerCase()] || parent[def]; | ||
} | ||
} | ||
Chain.prototype.__getCallbacksObjects = function(type, options, all) { | ||
var self = this; | ||
if (self['_' + type]) { | ||
var callbacksPaths = [self['_' + type]]; | ||
self._optionsName.forEach(function(name, i) { | ||
var tempPaths = []; | ||
callbacksPaths.forEach(function(path) { | ||
var param = options[name]; | ||
if (all) { | ||
if (path[self._ignore]) { | ||
tempPaths.push(path[self._ignore]); | ||
} | ||
if (param !== self._ignore && path[param]) { | ||
tempPaths.push(path[param]); | ||
} | ||
} else { | ||
if ((param !== self._ignore) && path[param]) { | ||
tempPaths.push(path[param]); | ||
} else { | ||
if (path[self._ignore]) { | ||
tempPaths.push(path[self._ignore]); | ||
} | ||
} | ||
} | ||
}); | ||
callbacksPaths = tempPaths; | ||
}); | ||
return callbacksPaths; | ||
} | ||
return null; | ||
return []; | ||
}; | ||
module.exports = Chain; | ||
module.exports = Chain; |
@@ -130,3 +130,2 @@ 'use strict'; | ||
* @param resourceId resource id | ||
* @param ops options {parent, method, name} | ||
*/ | ||
@@ -149,3 +148,3 @@ Traversal.prototype.getResourceChain = function(resourceId) { | ||
if (this.chains[resourceId]) { | ||
return this.chains[resourceId].getCallbacks(ops.name, ops.parent, ops.method); | ||
return this.chains[resourceId].getCallbacks(ops); | ||
} | ||
@@ -218,6 +217,11 @@ return null; | ||
req.buildResourceUrl = traversal.buildResourceUrl; | ||
var isXhr = function(){ | ||
var val = req.headers['X-Requested-With'] || req.headers['x-requested-with'] || ''; | ||
return 'xmlhttprequest' == val.toLowerCase(); | ||
}; | ||
var callbacks = traversal.getResourcePath(resource.resource, { | ||
name: req.pathname, | ||
name: req.pathname || '', | ||
method: req.method, | ||
parent: resource.parent ? resource.parent.resource : null | ||
parent: resource.parent ? resource.parent.resource : null, | ||
xhr: isXhr() | ||
}); | ||
@@ -224,0 +228,0 @@ if (callbacks && callbacks.length) { |
{ | ||
"name": "connect-traversal", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"description": "Middleware for Connect and Express application which allows to use URL traversal instead of URL dispatching.", | ||
@@ -40,4 +40,4 @@ "keywords": [ | ||
"readmeFilename": "README.md", | ||
"_id": "connect-traversal@0.3.2", | ||
"_id": "connect-traversal@0.3.3", | ||
"_from": "connect-traversal@latest" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
384
67226
16
2
0