Skip to content

Commit 3ab1983

Browse files
committed
Updated GROUP() by adding prefixes.
1 parent 66b0577 commit 3ab1983

5 files changed

Lines changed: 66 additions & 5 deletions

File tree

changes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- added: `CONVERT(obj, schema)` for quick converting values like Schema (more in docs.)
77
- added: `Capitalize2` schema type which converts only the first char
88

9+
- updated: `GROUP()` by adding a new argument `url_prefix`
10+
911
- fixed: mail `message.manually()`
1012

1113
======= 2.9.3 (HOTFIX)

index.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ var IMAGEMAGICK = false;
269269
var _controller = '';
270270
var _owner = '';
271271
var _flags;
272+
var _prefix;
272273

273274
// GO ONLINE MODE
274275
!global.framework_internal && (global.framework_internal = require('./internal'));
@@ -305,7 +306,6 @@ global.FINISHED = framework_internal.onFinished;
305306
global.DESTROY = framework_internal.destroyStream;
306307
global.UID = () => UIDGENERATOR.date + (++UIDGENERATOR.index).padLeft(4, '0') + UIDGENERATOR.instance + (UIDGENERATOR.index % 2 ? 1 : 0);
307308
global.ROUTE = (a, b, c, d, e) => F.route(a, b, c, d, e);
308-
global.GROUP = (a, b) => F.group(a, b);
309309
global.WEBSOCKET = (a, b, c, d) => F.websocket(a, b, c, d);
310310
global.FILE = (a, b, c) => F.file(a, b, c);
311311
global.REDIRECT = (a, b, c, d) => F.redirect(a, b, c, d);
@@ -1530,9 +1530,35 @@ global.CORS = F.cors = function(url, flags, credentials) {
15301530
return F;
15311531
};
15321532

1533-
F.group = function(flags, fn) {
1534-
_flags = flags;
1535-
fn.call(F);
1533+
global.GROUP = F.group = function() {
1534+
1535+
var fn = null;
1536+
1537+
_flags = null;
1538+
_prefix = null;
1539+
1540+
for (var i = 0; i < arguments.length; i++) {
1541+
var o = arguments[i];
1542+
1543+
if (o instanceof Array) {
1544+
_flags = o;
1545+
continue;
1546+
}
1547+
1548+
switch (typeof(o)) {
1549+
case 'string':
1550+
if (o.endsWith('/'))
1551+
o = o.substring(0, o.length - 1);
1552+
_prefix = o;
1553+
break;
1554+
case 'function':
1555+
fn = o;
1556+
break;
1557+
}
1558+
}
1559+
1560+
fn && fn.call(F);
1561+
_prefix = undefined;
15361562
_flags = undefined;
15371563
return F;
15381564
};
@@ -1610,6 +1636,9 @@ F.web = F.route = function(url, funcExecute, flags, length, language) {
16101636
if (url[0] !== '[' && url[0] !== '/')
16111637
url = '/' + url;
16121638

1639+
if (_prefix)
1640+
url = _prefix + url;
1641+
16131642
if (url.endsWith('/'))
16141643
url = url.substring(0, url.length - 1);
16151644

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"name": "Tema Smirnov",
9191
"email": "github.tema@smirnov.one"
9292
}],
93-
"version": "3.0.0-1",
93+
"version": "3.0.0-2",
9494
"homepage": "http://www.totaljs.com",
9595
"bugs": {
9696
"url": "https://github.com/totaljs/framework/issues",

test/controllers/default.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ exports.install = function() {
2626
this.plain('ROBOT');
2727
}, ['robot']);
2828

29+
GROUP(['get'], '/prefix1/', function() {
30+
ROUTE('/test/', function() {
31+
this.plain('PREFIX1TEST');
32+
});
33+
});
34+
35+
GROUP('prefix2', ['get'], function() {
36+
ROUTE('/test/', function() {
37+
this.plain('PREFIX2TEST');
38+
});
39+
});
40+
2941
F.route('#route');
3042
F.route('/view-in-modules/', '.' + F.path.modules('someview'));
3143
F.route('/options/', plain_options, ['options']);

test/test-framework-debug.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,24 @@ function test_routing(next) {
397397
});
398398
});
399399

400+
async.await('prefix -- 1', function(complete) {
401+
utils.request(url + 'prefix1/test/', ['get'], function(error, data) {
402+
if (error)
403+
throw error;
404+
assert.ok(data === 'PREFIX1TEST', 'Group + Prefix 1');
405+
complete();
406+
});
407+
});
408+
409+
async.await('prefix -- 2', function(complete) {
410+
utils.request(url + 'prefix2/test/', ['get'], function(error, data) {
411+
if (error)
412+
throw error;
413+
assert.ok(data === 'PREFIX2TEST', 'Group + Prefix 2');
414+
complete();
415+
});
416+
});
417+
400418
async.await('package/', function(complete) {
401419
utils.request(url + 'package/', ['get'], function(error, data, code, headers) {
402420
if (error)

0 commit comments

Comments
 (0)