Skip to content

Commit

Permalink
Added baseUrl to Router for blocks.middleware()
Browse files Browse the repository at this point in the history
  • Loading branch information
Joscha Rohmann committed Dec 9, 2015
1 parent cdc74ea commit 2af9161
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/modules/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
function Router() {
this._currentRoute = {};
this._routes = {};
this._baseUrl = '';
}

blocks.core.Router = Router;
Expand Down Expand Up @@ -170,6 +171,10 @@
}
});

if (this._baseUrl) {
result = this._baseUrl + result;
}

return result;
},

Expand All @@ -180,6 +185,10 @@

url = decodeURI(url);

if (this._baseUrl && url.startsWith(this._baseUrl)) {
url = url.substr(this._baseUrl.length, url.length);
}

blocks.each(this._routes, function (routeMetadata) {
blocks.each(routeMetadata.regExCollection, function (regEx) {
if (regEx.regEx.test(url)) {
Expand All @@ -189,7 +198,7 @@
id: routeMetadata.route._routeString,
params: getUrlParams(routeMetadata, regEx.params, matches)
});
routeMetadata = routeMetadata.parent;
routeMetadata = routeMetadata.parent;
}
return false;
}
Expand Down Expand Up @@ -380,6 +389,17 @@
});
}
});
},
_setBaseUrl: function (url) {
if (blocks.isString(url)) {
if (!url.endsWith('/')) {
url += '/';
}
if(url.startsWith('/')) {
url = url.substr(1, url.length);
}
this._baseUrl = url;
}
}
};

Expand Down
5 changes: 5 additions & 0 deletions src/mvc/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ define([
if (!this._started) {
this._started = true;
this._serverData = window.__blocksServerData__;

if (this._serverData && this._serverData.baseUrl) {
this._router._setBaseUrl(this._serverData.baseUrl);
}

this._createViews();
blocks.domReady(blocks.bind(this._ready, this, element));
}
Expand Down
6 changes: 5 additions & 1 deletion src/node/BrowserEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ define([
location[name] = props[name];
});
},

setBaseUrl: function (url) {
if (url) {
this._env.__baseUrl__ = url;
}
},
addElementsById: function (elementsById) {
var env = this._env;
env.document.__elementsById__ = elementsById;
Expand Down
4 changes: 3 additions & 1 deletion src/node/Middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ define([
next();
} else {
res.send(contents);
return;
}
});
},
Expand Down Expand Up @@ -100,6 +101,7 @@ define([
_createBrowserEnv: function (req, server) {
var browserEnv = BrowserEnv.Create();

browserEnv.setBaseUrl(req.baseUrl);
browserEnv.fillLocation(this._getLocation(req));
browserEnv.addElementsById(this._elementsById);
browserEnv.fillServer(server);
Expand All @@ -108,7 +110,7 @@ define([
},

_getLocation: function (req) {
return req.protocol + '://' + req.get('host') + req.url;
return req.protocol + '://' + req.get('host') + req.originalUrl;
}
};
});
1 change: 1 addition & 0 deletions src/node/createBrowserEnvObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ define(function () {
var timeoutId = 0;
var windowObj = {
__mock__: true,
__baseUrl__: '',

console: createConsoleMock(),

Expand Down
10 changes: 10 additions & 0 deletions src/node/overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,14 @@ define([
this.callSuccess(contents);
}
};

var blocksApplication = blocks.Application;

blocks.Application = function (options) {
var app = blocksApplication(options);
app._router._setBaseUrl(window.__baseUrl__);
server.data.baseUrl = window.__baseUrl__;
return app;
};

});

0 comments on commit 2af9161

Please sign in to comment.