Skip to content

Commit 3aeeb32

Browse files
committed
Add controller.params.
1 parent 88827f4 commit 3aeeb32

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ global.$$$ = global.GETSCHEMA = (group, name, fn, timeout) => framework_builders
297297
global.CREATE = (group, name) => framework_builders.getschema(group, name).default();
298298
global.SCRIPT = (body, value, callback, param) => F.script(body, value, callback, param);
299299
global.SINGLETON = (name, def) => SINGLETONS[name] || (SINGLETONS[name] = (new Function('return ' + (def || '{}')))());
300-
global.EACHSCHEMA = (group, fn) => framework_builders.eachschema(group, fn);
301300
global.FUNCTION = (name) => F.functions[name];
302301
global.ROUTING = (name) => F.routing(name);
303302
global.SCHEDULE = (date, each, fn, param) => F.schedule(date, each, fn, param);
@@ -1936,6 +1935,7 @@ F.web = F.route = function(url, funcExecute, flags, length, language) {
19361935
var hash = url2.hash();
19371936
var routeURL = framework_internal.routeSplitCreate(url2);
19381937
var arr = [];
1938+
var params = [];
19391939
var reg = null;
19401940
var regIndex = null;
19411941

@@ -1947,6 +1947,9 @@ F.web = F.route = function(url, funcExecute, flags, length, language) {
19471947
arr.push(i);
19481948

19491949
var sub = o.substring(1, o.length - 1);
1950+
var name = o.substring(1, o.length - 1).trim();
1951+
1952+
params.push(name);
19501953

19511954
if (sub[0] !== '/')
19521955
return;
@@ -1960,6 +1963,7 @@ F.web = F.route = function(url, funcExecute, flags, length, language) {
19601963
regIndex = [];
19611964
}
19621965

1966+
params[params.length - 1] = 'regexp' + (regIndex.length + 1);
19631967
reg[i] = new RegExp(sub.substring(1, index), sub.substring(index + 1));
19641968
regIndex.push(i);
19651969
});
@@ -2042,6 +2046,7 @@ F.web = F.route = function(url, funcExecute, flags, length, language) {
20422046
r.urlraw = urlraw;
20432047
r.url = routeURL;
20442048
r.param = arr;
2049+
r.paramnames = params.length ? params : null;
20452050
r.flags = flags || EMPTYARRAY;
20462051
r.flags2 = flags_to_object(flags);
20472052
r.method = method;
@@ -9589,6 +9594,23 @@ Controller.prototype = {
95899594

95909595
get sitemapid() {
95919596
return this.$sitemapid || this.route.sitemap;
9597+
},
9598+
9599+
get params() {
9600+
if (this.$params)
9601+
return this.$params;
9602+
var route = this.req.$total_route;
9603+
var names = route.paramnames;
9604+
if (names) {
9605+
var obj = {};
9606+
for (var i = 0; i < names.length; i++)
9607+
obj[names[i]] = this.req.split[route.param[i]];
9608+
this.$params = obj;
9609+
return obj;
9610+
} else {
9611+
this.$params = EMPTYOBJECT;
9612+
return EMPTYOBJECT;
9613+
}
95929614
}
95939615
};
95949616

test/controllers/default.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ exports.install = function() {
5656
F.route('/cookie/', view_cookie);
5757
F.route('/layout/', view_layout);
5858
F.route('/custom/', viewCustomTesting);
59-
F.route('/views/', viewViews, ["#middleware"]);
59+
F.route('/views/', viewViews, ['#middleware']);
6060
F.route('/view-notfound/', viewError);
6161
F.route('/views-if/', viewViewsIf);
6262
F.route('/{a}/', viewRouteA);
@@ -627,8 +627,10 @@ function viewRouteA() {
627627
self.plain('OK');
628628
}
629629

630-
function viewRouteAB() {
630+
function viewRouteAB(a, b) {
631631
var self = this;
632+
var params = self.params;
633+
assert.ok(params.a === a, params.b === b, 'controller.params');
632634
assert.ok(self.url === '/c/b/', 'routing: viewRouteAB');
633635
self.plain('OK');
634636
}

0 commit comments

Comments
 (0)