Skip to content
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
36 changes: 27 additions & 9 deletions blocks/i-router/i-router.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,35 +103,46 @@ BEM.decl('i-router', null, {
*
* @param [{String|Array} reqMethod request method: 'get', 'post', 'get,post', 'delete' etc. or list of arguments
* To assign more than one method list with comma]
* @param {String|RegExp} reqHost hostname
* @param {String|RegExp} reqPath request path matcher
* @param {String} blockName block name
*/
define: function (reqMethod, reqPath, blockName) {
if (Array.isArray(reqMethod) && !reqPath) {
reqMethod.forEach(function (routeDefn) {
define: function (definitions) {
var blockName,
reqPath,
reqMethod = false,
reqHost = false;

if (Array.isArray(definitions) && arguments.length === 1) {
definitions.forEach(function (routeDefn) {
this.define.apply(this, routeDefn);
}, this);

return;
}
blockName = [].pop.call(arguments);
reqPath = [].pop.call(arguments);
if (arguments.length > 0) {
reqMethod = [].pop.call(arguments);
}
if (arguments.length > 0) {
reqHost = [].pop.call(arguments);
}

if (blockName) {
if (reqMethod) {
reqMethod = reqMethod.toUpperCase();
reqMethod.split(',').every(function (method) {
if (-1 === this.REQUEST_METHODS.indexOf(method)) {
throw new Error('Unexpected request method: ' + method);
}
return true;
}, this);
} else {
blockName = reqPath;
reqPath = reqMethod;
reqMethod = false;
}

this._routeList.push({
reqMethod: reqMethod,
reqPath: reqPath,
reqHost: reqHost,
reqHandler: this._createHandler(blockName)
});
},
Expand All @@ -145,7 +156,7 @@ BEM.decl('i-router', null, {
* @return {Function} .handler request handler
* @return {Array} .matchers
*/
_getRoute: function (path, method) {
_getRoute: function (path, method, hostName) {
var route,
matchers = [],
pathNorm = path.replace(/\/{2,}/g, '/');
Expand All @@ -156,6 +167,13 @@ BEM.decl('i-router', null, {
return false;
}
}
if (hostName && r.reqHost) {
if (typeof (r.reqHost) === 'string' && hostName !== r.hostName) {
return false;
} else if (!hostName.match(r.reqHost)) {
return false;
}
}

if (typeof (r.reqPath) === 'string') {
if (pathNorm === r.reqPath) {
Expand Down
27 changes: 16 additions & 11 deletions blocks/i-router/i-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

this._state.set('path', getPathFromLocation());
this._lastPath = this.getPath();
this._lastHandler = this._prepearRoute(this._lastPath);
this._lastHost = this.getHost();
this._lastHandler = this._prepearRoute(this._lastPath, this._lastHost);
if (this._historyStateSupported()) {
jQuery(document).delegate('a', 'click', function (e) {
if (!e.metaKey && !e.ctrlKey && this.protocol === location.protocol
Expand Down Expand Up @@ -60,7 +61,8 @@
define: function () {
this.__base.apply(this, arguments);
this._lastPath = this.getPath();
this._lastHandler = this._prepearRoute(this._lastPath);
this._lastHost = this.getHost();
this._lastHandler = this._prepearRoute(this._lastPath, this._lastHost);
},

/**
Expand Down Expand Up @@ -145,12 +147,14 @@
* Process handler for given path
*/
_onChange: function () {
var currentPath = this.getPath(), handler;
var currentPath = this.getPath(),
currentHost = this.getHost(),
handler;

if (this._lastPath !== currentPath) {
if (this._lastPath !== currentPath || this._lastHost !== currentHost) {
handler = this._prepearRoute();
BEM.channel('i-router').trigger('update', {path: currentPath}); //deprecated
this.trigger('update', {path: currentPath});
BEM.channel('i-router').trigger('update', {path: currentPath, host: currentHost}); //deprecated
this.trigger('update', {path: currentPath, host: currentHost});
if (handler) {
this._execHandler(handler)
.fail(this.reload)
Expand All @@ -159,7 +163,7 @@
this.missing();
}
this._lastPath = currentPath;

this._lastHost = currentHost;
}
},

Expand All @@ -171,8 +175,9 @@
*
* @return {Object} handler
*/
_prepearRoute: function (path) {
var routePath = path || (location.pathname + location.search),
_prepearRoute: function (path, host) {
var routePath = path || getPathFromLocation(),
routeHost = host || this.getHost(),
idx = routePath.indexOf('?'),
pathName, search, routeInfo;

Expand All @@ -184,8 +189,8 @@
pathName = routePath;
search = '';
}

routeInfo = this._getRoute(pathName);
//we are at client side, only GET method's are possible
routeInfo = this._getRoute(pathName, 'GET', routeHost);

this._state.set('matchers', (routeInfo) ? routeInfo.matchers : []);
this._state.set('path', routePath);
Expand Down
2 changes: 1 addition & 1 deletion blocks/i-router/i-router.priv.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
* @param {http.ServerResponse} res
*/
_onRequest: function (req, res) {
var routeInfo = this._getRoute(url.parse(req.url).pathname, req.method),
var routeInfo = this._getRoute(url.parse(req.url).pathname, req.method, req.headers.host),
reqDomain = domain.create(),
_this = this;

Expand Down