Permalink
Browse files

Merge pull request #854 from caridy/micro-tunning

fix for ac.url.make weird behavior after micro-optmiziation
  • Loading branch information...
caridy committed Dec 12, 2012
2 parents 5c54fb4 + 45f0095 commit 7c48652eab300487153a4e5aba2b7bd372936c11
@@ -14,28 +14,6 @@
*/
YUI.add('mojito-url-addon', function(Y, NAME) {
- function objectToQueryStr(obj, removeEmpty) {
- var sanitized = {},
- key;
- // If "removeEmpty" is true we remove any params with no value.
- if (removeEmpty) {
- // by creating a copy with valid values we gain
- // a little bit with V8
- for (key in obj) {
- if (obj.hasOwnProperty(key) && obj[key]) {
- sanitized[key] = obj[key];
- }
- }
- obj = sanitized;
- }
-
- if (Y.Lang.isObject(obj) && Y.Object.size(obj) > 0) {
- obj = Y.QueryString.stringify(obj);
- }
-
- return obj;
- }
-
/**
* <strong>Access point:</strong> <em>ac.url.*</em>
@@ -45,7 +23,7 @@ YUI.add('mojito-url-addon', function(Y, NAME) {
function UrlAcAddon(command, adapter, ac) {
this.context = command.context;
this.rs = null;
- this.pathToRoot = ac.config.getAppConfig().pathToRoot;
+ this.pathToRoot = ac.staticAppConfig.pathToRoot;
this.maker = null;
}
@@ -61,39 +39,46 @@ 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.
+ * @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;
- routeParams = objectToQueryStr(routeParams || {});
-
- if (routeParams && routeParams.length) {
- query = query + '?' + routeParams;
- }
-
- url = this.getRouteMaker().make(query, verb);
+ params = (typeof routeParams === 'string' ? Y.QueryString.parse(routeParams) :
+ Y.merge(routeParams || {}));
if (urlParams) {
- urlParams = objectToQueryStr(urlParams, true);
-
- if (urlParams && urlParams.length) {
- url = url + '?' + urlParams;
+ 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];
+ }
}
+
}
+ url = this.getRouteMaker().make(query, (verb || 'GET').toUpperCase(), params);
+
// IOS PATCH
- if (typeof window !== 'undefined') {
+ if (url && (typeof window !== 'undefined')) {
url = Y.mojito.util.iOSUrl(url);
}
// this is mainly used by html5app
- if (this.pathToRoot) {
+ if (url && this.pathToRoot) {
url = this.pathToRoot + url;
}
@@ -105,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) {
@@ -119,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() {
@@ -146,8 +131,8 @@ YUI.add('mojito-url-addon', function(Y, NAME) {
Y.namespace('mojito.addons.ac').url = UrlAcAddon;
}, '0.1.0', {requires: [
- 'querystring',
'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 = {};
@@ -218,18 +217,22 @@ YUI.add('mojito-route-maker', function(Y, NAME) {
* @method make
* @param {String} query string to convert to a URL
* @param {String} verb http method
+ * @param {Object} params object representing extra querystring
+ * params. `query` might have querystring portion
+ * portion, in which case they have priority.
*/
- make: function(query, verb) {
+ make: function(query, verb, params) {
// Y.log('make(' + query + ', ' + verb + ')', 'debug', NAME);
var parts = query.split('?'),
call = parts[0],
- params = {},
residual = {},
route,
uri,
k;
+ params = params || {};
+
// TODO: don't assign to a parameter.
verb = verb || 'GET';
@@ -84,7 +84,7 @@ YUI.add('PagedFlickr', function(Y) {
for (var k in mods) {
params[k] = mods[k];
}
- return ac.url.make(mojitType, 'index', Y.QueryString.stringify(params));
+ return ac.url.make(mojitType, 'index', params);
}
-}, '0.0.1', {requires: ['mojito-intl-addon', 'mojito-util', 'querystring-stringify', 'ModelFlickr'], lang: ['de', 'en-US']});
+}, '0.0.1', {requires: ['mojito-intl-addon', 'mojito-util', 'ModelFlickr'], lang: ['de', 'en-US']});
@@ -84,7 +84,7 @@ YUI.add('PagedFlickr', function(Y) {
for (var k in mods) {
params[k] = mods[k];
}
- return ac.url.make(mojitType, 'index', Y.QueryString.stringify(params));
+ return ac.url.make(mojitType, 'index', params);
}
-}, '0.0.1', {requires: ['mojito-intl-addon', 'mojito-util', 'querystring-stringify', 'ModelFlickr'], lang: ['de', 'en-US']});
+}, '0.0.1', {requires: ['mojito-intl-addon', 'mojito-util', 'ModelFlickr'], lang: ['de', 'en-US']});
@@ -84,14 +84,13 @@ YUI.add('PagedFlickr', function(Y) {
for (var k in mods) {
params[k] = mods[k];
}
- return ac.url.make(mojitType, 'index', Y.QueryString.stringify(params));
+ return ac.url.make(mojitType, 'index', params);
}
}, '0.0.1', {requires: [
'mojito-intl-addon',
'mojito-params-addon',
'mojito-url-addon',
'mojito-util',
- 'querystring-stringify',
'ModelFlickr'
], lang: ['de', 'en-US']});
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2011 Yahoo! Inc. All rights reserved.
*/
-YUI.add('ShowUrl', function(Y, NAME) {
+YUI.add('ShowUrl', function (Y, NAME) {
/**
* The ShowUrl module.
@@ -23,68 +23,72 @@ YUI.add('ShowUrl', function(Y, NAME) {
* @param ac {Object} The action context that provides access
* to the Mojito API.
*/
- index: function(ac) {
- var mojitName = ac.params.getFromUrl('mojit_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'),
- nameExists = mojitName ? "YES" : "NO",
- actionExists = mojitAction ? "YES" : "NO";
- urlparamsExists = mojitUrlParams ? "YES" : "NO";
- urlExists = mojitUrl ? "YES" : "NO";
+ index: function (ac) {
+ var mojitName = ac.params.getFromUrl('mojit_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'),
+ nameExists = mojitName ? "YES" : "NO",
+ actionExists = mojitAction ? "YES" : "NO",
+ urlparamsExists = mojitUrlParams ? "YES" : "NO",
+ urlExists = mojitUrl ? "YES" : "NO",
+ url,
+ data,
+ matchroute;
- console.log("**********************" + mojitName);
- console.log("**********************" + mojitAction);
- console.log("**********************" + mojitUrlParams);
- console.log("**********************" + mojitUrl);
- console.log("**********************" + mojitVerb);
+ console.log("**********************" + mojitName);
+ console.log("**********************" + mojitAction);
+ console.log("**********************" + mojitUrlParams);
+ console.log("**********************" + mojitUrl);
+ console.log("**********************" + mojitVerb);
- if (nameExists === "YES" && actionExists === "YES")
- {
- var url = "";
- var error = "";
- try{
- if (urlparamsExists === "YES"){
- url = ac.url.make(mojitName, mojitAction, 'MySpecialRoute', 'GET', mojitUrlParams);
- } else {
- url = ac.url.make(mojitName, mojitAction);
- }
- }catch(error) {
- url = error;
- }
- var data = {
- url: url,
- name: mojitName,
- action: mojitAction
- };
- //ac.http.setHeader('content-type', 'text/html');
- ac.done(data);
- } else if (urlExists === "YES"){
- var matchroute = "";
- var error = "";
- try{
- matchroute = ac.url.find(mojitUrl, mojitVerb);
- }catch(error) {
- url = error;
- }
- console.log(matchroute);
- if(matchroute != null){
- var data = {
- url: mojitUrl,
- verbs: matchroute.verbs.GET,
- call: matchroute.call,
- name: matchroute.name,
- params: matchroute.params.secret,
- }
- }else{
- var data = {
- url: mojitUrl,
- }
+ if (nameExists === "YES" && actionExists === "YES") {
+ url = "";
+ try {
+ if (urlparamsExists === "YES") {
+ url = ac.url.make(mojitName, mojitAction, {
+ id: 'MySpecialRoute'
+ }, mojitVerb, {
+ u: mojitUrlParams
+ });
+ } else {
+ url = ac.url.make(mojitName, mojitAction);
+ }
+ } catch (err1) {
+ url = err1;
}
- ac.done(data);
- }
- }
+ data = {
+ url: url,
+ name: mojitName,
+ action: mojitAction
+ };
+ //ac.http.setHeader('content-type', 'text/html');
+ ac.done(data);
+ } else if (urlExists === "YES") {
+ matchroute = "";
+ try {
+ matchroute = ac.url.find(mojitUrl, mojitVerb);
+ } catch (err2) {
+ url = err2;
+ }
+ console.log(matchroute);
+ if (matchroute !== null) {
+ data = {
+ url: mojitUrl,
+ verbs: matchroute.verbs.GET,
+ call: matchroute.call,
+ name: matchroute.name,
+ params: matchroute.params.secret
+ };
+ } else {
+ data = {
+ url: mojitUrl
+ };
+ }
+ ac.done(data);
+ }
+ }
};
}, '0.0.1', {requires: [
@@ -41,17 +41,17 @@ YUI.add('PagedFlickr', function(Y, NAME) {
data.has_prev = true;
}
ac.done(data);
-
+
});
}
};
-
+
function selfUrl(ac, mods) {
var params = Y.mojito.util.copy(ac.params.getFromMerged());
for (var k in mods) {
params[k] = mods[k];
}
- return ac.url.make('flickr', 'index', Y.QueryString.stringify(params));
+ return ac.url.make('flickr', 'index', params);
}
@@ -51,7 +51,7 @@ YUI.add('PagedFlickr2', function(Y, NAME) {
for (var k in mods) {
params[k] = mods[k];
}
- return ac.url.make('flickr2', 'index', Y.QueryString.stringify(params));
+ return ac.url.make('flickr2', 'index', params);
}
@@ -74,7 +74,7 @@ YUI.add('PagedFlickr5', function(Y, NAME) {
for (var k in mods) {
params[k] = mods[k];
}
- return ac.url.make('flickr5', 'index', Y.QueryString.stringify(params));
+ return ac.url.make('flickr5', 'index', params);
}
}, '0.0.1', {requires: [
@@ -84,5 +84,4 @@ YUI.add('PagedFlickr5', function(Y, NAME) {
'mojito-url-addon',
'mojito-intl-addon',
'mojito-util',
- 'querystring-stringify',
'PagedFlickr5Model'], lang: ['de', 'en-US']});
Oops, something went wrong.

0 comments on commit 7c48652

Please sign in to comment.