Permalink
Browse files

addressing review notes from @folta

  • Loading branch information...
caridy committed Dec 12, 2012
1 parent 9619f96 commit 95875cb851a184980cab7ba6d87bd018fc1d61cc
@@ -39,36 +39,38 @@ YUI.add('mojito-url-addon', function(Y, NAME) {
* Mojito application configuration.
* @param {string} action Action reference, concatenated to the base
* using a period (.) separator.
- * @param {object} routeParams used to lookup the route in the routing
- * table.
- * @param {string} varb GET, POST, PUT, DELETE (case insensitive).
- * @param {object} urlParams added to the looked up route as query
- * params, this has priority over routeParams.
+ * @param {object|string} routeParams used to lookup the route in the routing
+ * table. If string is provided, Y.QueryString.parse will be used.
+ * @param {string} verb the HTTP method. Default value is "get".
+ * @param {object|string} urlParams added to the looked up route as query
+ * params, this has priority over routeParams. If string is provided,
+ * Y.QueryString.parse will be used.
*/
make: function(base, action, routeParams, verb, urlParams) {
var url,
key,
params,
query = base + '.' + action;
- params = Y.merge(routeParams || {});
+ params = (typeof routeParams === 'string' ? Y.QueryString.parse(routeParams) :
+ Y.merge(routeParams || {}));
if (urlParams) {
- if (Y.Lang.isObject(urlParams)) {
- // adding querystring params to routeParams and let
- // the url maker to create the proper url. Empty params
- // will be left out. TODO: why?
- for (key in urlParams) {
- if (urlParams.hasOwnProperty(key) && urlParams[key]) {
- params[key] = urlParams[key];
- }
+ urlParams = (typeof urlParams === 'string' ? Y.QueryString.parse(urlParams) :
+ urlParams);
+
+ // adding querystring params to routeParams and let
+ // the url maker to create the proper url. Empty params
+ // will be left out. TODO: why?
+ for (key in urlParams) {
+ if (urlParams.hasOwnProperty(key) && urlParams[key]) {
+ params[key] = urlParams[key];
}
- } else {
- Y.log('ac.url.make is expecting an object as urlParams instead of: ' + urlParams, 'warn', NAME);
}
+
}
- url = this.getRouteMaker().make(query, verb, params);
+ url = this.getRouteMaker().make(query, (verb || 'GET').toUpperCase(), params);
// IOS PATCH
if (url && (typeof window !== 'undefined')) {
@@ -88,7 +90,7 @@ YUI.add('mojito-url-addon', function(Y, NAME) {
* Finds the first matching route from the given URL
* @method find
* @param {string} url the URL to find a route for.
- * @param {string} verb the HTTP method.
+ * @param {string} the HTTP method. Default value is "get".
*/
find: function(url, verb) {
@@ -102,7 +104,7 @@ YUI.add('mojito-url-addon', function(Y, NAME) {
url = url.slice(0, url.indexOf('?'));
}
- return this.getRouteMaker().find(url, verb);
+ return this.getRouteMaker().find(url, (verb || 'GET').toUpperCase());
},
getRouteMaker: function() {
@@ -131,5 +133,6 @@ YUI.add('mojito-url-addon', function(Y, NAME) {
}, '0.1.0', {requires: [
'mojito-config-addon',
'mojito-route-maker',
+ 'querystring-parse-simple',
'mojito-util'
]});
@@ -72,9 +72,8 @@ YUI.add('mojito-route-maker', function(Y, NAME) {
// not recomputed.
if (route.verbs.length && route.path && route.call) {
- // FUTURE: [Issue 73] allow object params, not just string
if (!route.params) {
- route.params = '';
+ route.params = {};
}
if (!route.regex) {
route.regex = {};
@@ -28,7 +28,7 @@ YUI.add('ShowUrl', function (Y, NAME) {
mojitAction = ac.params.getFromUrl('mojit_action'),
mojitUrlParams = ac.params.getFromUrl('mojit_urlparams'),
mojitUrl = ac.params.getFromUrl('mojit_url'),
- mojitVerb = (ac.params.getFromUrl('mojit_verb') || 'get').toLowerCase(),
+ mojitVerb = ac.params.getFromUrl('mojit_verb'),
nameExists = mojitName ? "YES" : "NO",
actionExists = mojitAction ? "YES" : "NO",
urlparamsExists = mojitUrlParams ? "YES" : "NO",
@@ -11,7 +11,7 @@ YUI({
suite.add(new Y.Test.Case({
"test BasicRouting10-1": function(){
Y.Assert.areEqual('Click to execute the action \'route-2\' for the mojit \'nothing\'', Y.one('#mylink').get('innerHTML'));
- Y.Assert.areEqual("error:%20No%20route%20match%20found%20for%20'route-2.nothing'%20(get)", Y.one('#mylink').get('href'), 'invalid routes should trigger an error');
+ Y.Assert.areEqual('/route-2/nothing', Y.one('#mylink').get('href').match(/\/route-2\/nothing/gi));
Y.Assert.areEqual('route-2', Y.one('#name').get('innerHTML'));
}
}));
@@ -35,7 +35,7 @@ YUI().use('mojito-url-addon', 'test', 'querystring', function(Y) {
return {
find: function(url, verb) {
A.areSame('myid.myaction', url);
- A.areSame('get', verb);
+ A.areSame('GET', verb);
return 'ohhai url';
}
};
@@ -117,7 +117,7 @@ YUI().use('mojito-url-addon', 'test', 'querystring', function(Y) {
return {
find: function(url, verb) {
A.areSame('myid.myaction', url);
- A.areSame('post', verb);
+ A.areSame('POST', verb);
return 'ohhai url';
}
};
@@ -138,7 +138,7 @@ YUI().use('mojito-url-addon', 'test', 'querystring', function(Y) {
make: function(query, verb, params) {
A.areSame('myid.myaction', query);
A.areSame('bar', params.foo);
- A.areSame('get', verb);
+ A.areSame('GET', verb);
return 'ohhai url';
}
};
@@ -162,7 +162,7 @@ YUI().use('mojito-url-addon', 'test', 'querystring', function(Y) {
A.areSame('bar', params.foo);
A.areSame(1, params.a);
A.areSame(2, params.b);
- A.areSame('get', verb);
+ A.areSame('GET', verb);
return 'ohhai url';
}
};
@@ -183,7 +183,7 @@ YUI().use('mojito-url-addon', 'test', 'querystring', function(Y) {
make: function(query, verb, params) {
A.areSame('myid.myaction', query);
A.areSame('bar', params.foo);
- A.areSame('post', verb);
+ A.areSame('POST', verb);
return 'ohhai url';
}
};
@@ -203,7 +203,7 @@ YUI().use('mojito-url-addon', 'test', 'querystring', function(Y) {
return {
make: function(query, verb) {
A.areSame('myid.myaction', query);
- A.isUndefined(verb);
+ A.areSame('GET', verb);
return 'ohhai url';
}
};
@@ -224,7 +224,7 @@ YUI().use('mojito-url-addon', 'test', 'querystring', function(Y) {
make: function(query, verb, params) {
A.areSame('myid.myaction', query);
A.areSame('bar', params.foo);
- A.areSame('get', verb);
+ A.areSame('GET', verb);
return 'ohhai url';
}
};
@@ -236,6 +236,98 @@ YUI().use('mojito-url-addon', 'test', 'querystring', function(Y) {
url = addon.make('myid', 'myaction', {foo:'bar'}, 'get');
A.areSame('ohhai url', url);
+ },
+
+ 'test default verb': function() {
+ Y.mojito.RouteMaker = function(rtes) {
+ A.areSame('routes', rtes);
+ return {
+ make: function(query, verb, params) {
+ A.areSame('myid.myaction', query);
+ A.areSame('GET', verb);
+ return 'ohhai url';
+ }
+ };
+ };
+ var addon = new Y.mojito.addons.ac.url({}, null, acMock);
+ addon.setStore({
+ getRoutes: function() { return 'routes'; }
+ });
+
+ url = addon.make('myid', 'myaction');
+ A.areSame('ohhai url', url);
+ },
+
+ 'test routeParams and queryParams priorities': function() {
+ Y.mojito.RouteMaker = function(rtes) {
+ A.areSame('routes', rtes);
+ return {
+ make: function(query, verb, params) {
+ A.areSame('myid.myaction', query);
+ A.areSame('baz', params.foo, 'queryParams should have the priority');
+ A.areSame('GET', verb);
+ return 'ohhai url';
+ }
+ };
+ };
+ var addon = new Y.mojito.addons.ac.url({}, null, acMock);
+ addon.setStore({
+ getRoutes: function() { return 'routes'; }
+ });
+
+ url = addon.make('myid', 'myaction', {foo: 'bar'}, 'get', {foo: 'baz'});
+ A.areSame('ohhai url', url);
+ },
+
+ 'test routeParams and queryParams as strings': function() {
+ Y.mojito.RouteMaker = function(rtes) {
+ A.areSame('routes', rtes);
+ return {
+ make: function(query, verb, params) {
+ A.areSame('myid.myaction', query);
+ A.areSame('bar', params.foo);
+ A.areSame('baz', params.bar);
+ A.areSame('1', params.a);
+ A.areSame('2', params.b);
+ A.areSame('GET', verb);
+ return 'ohhai url';
+ }
+ };
+ };
+ var addon = new Y.mojito.addons.ac.url({}, null, acMock);
+ addon.setStore({
+ getRoutes: function() { return 'routes'; }
+ });
+
+ url = addon.make('myid', 'myaction', 'foo=bar&bar=baz', 'get', 'a=1&b=2');
+ A.areSame('ohhai url', url);
+ },
+
+ 'test routeParams and queryParams as arrays': function() {
+ Y.mojito.RouteMaker = function(rtes) {
+ A.areSame('routes', rtes);
+ return {
+ make: function(query, verb, params) {
+ console.error(params);
+ A.areSame('myid.myaction', query);
+ A.areSame('a', params['0']);
+ A.areSame('b', params['1']);
+ A.isUndefined(params.foo);
+ A.isUndefined(params.bar);
+ A.isUndefined(params.a);
+ A.isUndefined(params.b);
+ A.areSame('GET', verb);
+ return 'ohhai url';
+ }
+ };
+ };
+ var addon = new Y.mojito.addons.ac.url({}, null, acMock);
+ addon.setStore({
+ getRoutes: function() { return 'routes'; }
+ });
+
+ url = addon.make('myid', 'myaction', ['foo', 'bar'], 'get', ['a', 'b']);
+ A.areSame('ohhai url', url);
}
};

0 comments on commit 95875cb

Please sign in to comment.