1
0
Fork 0

Foxx: Added the params function

This commit is contained in:
Lucas Dohmen 2013-03-23 16:01:01 +01:00
parent 1759b7d85a
commit 0dd44d82aa
2 changed files with 43 additions and 1 deletions

View File

@ -115,6 +115,11 @@ _.extend(FoxxApplication.prototype, {
// app.handleRequest("get", "/gaense", function (req, res) {
// //handle the request
// });
//
// When defining a route you can also define a so called 'parameterized'
// route like `/gaense/:stable`. In this case you can later get the value
// the user provided for `stable` via the `params` function (see the Request
// object).
handleRequest: function (method, route, argument1, argument2) {
'use strict';
var newRoute = {}, options, callback;
@ -316,9 +321,26 @@ BaseMiddleware = function (templateCollection, helperCollection) {
//
// FoxxApplication adds the following methods to this request object:
requestFunctions = {
// Get the body of the request.
// ### The superfluous `body` function
// Get the body of the request
body: function () {
return this.requestBody;
},
// ### The jinxed `params` function
// Get the parameters of the request. This process is two-fold:
//
// 1. If you have defined an URL like `/test/:id` and the user
// requested `/test/1`, the call `params("id")` will return `1`.
// 2. If you have defined an URL like `/test` and the user gives a
// query component, the query parameters will also be returned.
// So for example if the user requested `/test?a=2`, the call
// `params("a")` will return `2`.
params: function (key) {
var ps = {};
_.extend(ps, this.urlParameters);
_.extend(ps, this.parameters);
return ps[key];
}
};

View File

@ -302,6 +302,26 @@ function BaseMiddlewareWithoutTemplateSpec () {
assertEqual(request.body(), "test");
},
testParamFunctionReturnsUrlParameters: function () {
request.urlParameters = {a: 1};
baseMiddleware(request, response, options, next);
assertEqual(request.params("a"), 1);
},
testParamFunctionReturnsParameters: function () {
request.parameters = {a: 1};
baseMiddleware(request, response, options, next);
assertEqual(request.params("a"), 1);
},
testParamFunctionReturnsAllParams: function () {
request.urlParameters = {a: 1};
request.parameters = {b: 2};
baseMiddleware(request, response, options, next);
assertEqual(request.params("a"), 1);
assertEqual(request.params("b"), 2);
},
testStatusFunctionAddedToResponse: function () {
baseMiddleware(request, response, options, next);