Skip to content

Commit

Permalink
Query params v3
Browse files Browse the repository at this point in the history
  • Loading branch information
machty committed Mar 6, 2014
1 parent a1fd650 commit 21183ac
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 90 deletions.
41 changes: 29 additions & 12 deletions dist/commonjs/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ Router.prototype = {
// changed query params given that no activeTransition
// is guaranteed to have occurred.
this._changedQueryParams = queryParamChangelist.changed;
for (var k in queryParamChangelist.removed) {
if (queryParamChangelist.removed.hasOwnProperty(k)) {
this._changedQueryParams[k] = null;
}
}
trigger(this, newState.handlerInfos, true, ['queryParamsDidChange', queryParamChangelist.changed, queryParamChangelist.all, queryParamChangelist.removed]);
this._changedQueryParams = null;

Expand All @@ -84,14 +89,15 @@ Router.prototype = {
} else {
// Running queryParamsDidChange didn't change anything.
// Just update query params and be on our way.
oldState.queryParams = finalizeQueryParamChange(this, newState.handlerInfos, newState.queryParams);

// We have to return a noop transition that will
// perform a URL update at the end. This gives
// the user the ability to set the url update
// method (default is replaceState).
newTransition = new Transition(this);
newTransition.urlMethod = 'replace';

oldState.queryParams = finalizeQueryParamChange(this, newState.handlerInfos, newState.queryParams, newTransition);

newTransition.promise = newTransition.promise.then(function(result) {
updateURL(newTransition, oldState, true);
if (router.didTransition) {
Expand Down Expand Up @@ -179,7 +185,7 @@ Router.prototype = {
var args = slice.call(arguments);
if (url.charAt(0) !== '/') { args[0] = '/' + url; }

return doTransition(this, args).method('replaceQuery');
return doTransition(this, args).method(null);
},

/**
Expand Down Expand Up @@ -435,7 +441,7 @@ function setupContexts(router, newState, transition) {
throw e;
}

router.state.queryParams = finalizeQueryParamChange(router, currentHandlerInfos, newState.queryParams);
router.state.queryParams = finalizeQueryParamChange(router, currentHandlerInfos, newState.queryParams, transition);
}


Expand Down Expand Up @@ -569,14 +575,10 @@ function updateURL(transition, state, inputUrl) {
}

if (urlMethod) {
params.queryParams = state.queryParams;
params.queryParams = transition._visibleQueryParams || state.queryParams;
var url = router.recognizer.generate(handlerName, params);

if (urlMethod === 'replaceQuery') {
if (url !== inputUrl) {
router.replaceURL(url);
}
} else if (urlMethod === 'replace') {
if (urlMethod === 'replace') {
router.replaceURL(url);
} else {
router.updateURL(url);
Expand Down Expand Up @@ -702,21 +704,36 @@ function handlerInfosEqual(handlerInfos, otherHandlerInfos) {
return true;
}

function finalizeQueryParamChange(router, resolvedHandlers, newQueryParams) {
function finalizeQueryParamChange(router, resolvedHandlers, newQueryParams, transition) {
// We fire a finalizeQueryParamChange event which
// gives the new route hierarchy a chance to tell
// us which query params it's consuming and what
// their final values are. If a query param is
// no longer consumed in the final route hierarchy,
// its serialized segment will be removed
// from the URL.

for (var k in newQueryParams) {
if (newQueryParams.hasOwnProperty(k) &&
newQueryParams[k] === null) {
delete newQueryParams[k];
}
}

var finalQueryParamsArray = [];
trigger(router, resolvedHandlers, true, ['finalizeQueryParamChange', newQueryParams, finalQueryParamsArray]);
trigger(router, resolvedHandlers, true, ['finalizeQueryParamChange', newQueryParams, finalQueryParamsArray, transition]);

if (transition) {
transition._visibleQueryParams = {};
}

var finalQueryParams = {};
for (var i = 0, len = finalQueryParamsArray.length; i < len; ++i) {
var qp = finalQueryParamsArray[i];
finalQueryParams[qp.key] = qp.value;
if (transition && qp.visible !== false) {
transition._visibleQueryParams[qp.key] = qp.value;
}
}
return finalQueryParams;
}
Expand Down
1 change: 0 additions & 1 deletion dist/commonjs/router/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";
var slice = Array.prototype.slice;


var _isArray;
if (!Array.isArray) {
_isArray = function (x) {
Expand Down
42 changes: 29 additions & 13 deletions dist/router.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ define("router/router",
// changed query params given that no activeTransition
// is guaranteed to have occurred.
this._changedQueryParams = queryParamChangelist.changed;
for (var k in queryParamChangelist.removed) {
if (queryParamChangelist.removed.hasOwnProperty(k)) {
this._changedQueryParams[k] = null;
}
}
trigger(this, newState.handlerInfos, true, ['queryParamsDidChange', queryParamChangelist.changed, queryParamChangelist.all, queryParamChangelist.removed]);
this._changedQueryParams = null;

Expand All @@ -300,14 +305,15 @@ define("router/router",
} else {
// Running queryParamsDidChange didn't change anything.
// Just update query params and be on our way.
oldState.queryParams = finalizeQueryParamChange(this, newState.handlerInfos, newState.queryParams);

// We have to return a noop transition that will
// perform a URL update at the end. This gives
// the user the ability to set the url update
// method (default is replaceState).
newTransition = new Transition(this);
newTransition.urlMethod = 'replace';

oldState.queryParams = finalizeQueryParamChange(this, newState.handlerInfos, newState.queryParams, newTransition);

newTransition.promise = newTransition.promise.then(function(result) {
updateURL(newTransition, oldState, true);
if (router.didTransition) {
Expand Down Expand Up @@ -395,7 +401,7 @@ define("router/router",
var args = slice.call(arguments);
if (url.charAt(0) !== '/') { args[0] = '/' + url; }

return doTransition(this, args).method('replaceQuery');
return doTransition(this, args).method(null);
},

/**
Expand Down Expand Up @@ -651,7 +657,7 @@ define("router/router",
throw e;
}

router.state.queryParams = finalizeQueryParamChange(router, currentHandlerInfos, newState.queryParams);
router.state.queryParams = finalizeQueryParamChange(router, currentHandlerInfos, newState.queryParams, transition);
}


Expand Down Expand Up @@ -785,14 +791,10 @@ define("router/router",
}

if (urlMethod) {
params.queryParams = state.queryParams;
params.queryParams = transition._visibleQueryParams || state.queryParams;
var url = router.recognizer.generate(handlerName, params);

if (urlMethod === 'replaceQuery') {
if (url !== inputUrl) {
router.replaceURL(url);
}
} else if (urlMethod === 'replace') {
if (urlMethod === 'replace') {
router.replaceURL(url);
} else {
router.updateURL(url);
Expand Down Expand Up @@ -918,21 +920,36 @@ define("router/router",
return true;
}

function finalizeQueryParamChange(router, resolvedHandlers, newQueryParams) {
function finalizeQueryParamChange(router, resolvedHandlers, newQueryParams, transition) {
// We fire a finalizeQueryParamChange event which
// gives the new route hierarchy a chance to tell
// us which query params it's consuming and what
// their final values are. If a query param is
// no longer consumed in the final route hierarchy,
// its serialized segment will be removed
// from the URL.

for (var k in newQueryParams) {
if (newQueryParams.hasOwnProperty(k) &&
newQueryParams[k] === null) {
delete newQueryParams[k];
}
}

var finalQueryParamsArray = [];
trigger(router, resolvedHandlers, true, ['finalizeQueryParamChange', newQueryParams, finalQueryParamsArray]);
trigger(router, resolvedHandlers, true, ['finalizeQueryParamChange', newQueryParams, finalQueryParamsArray, transition]);

if (transition) {
transition._visibleQueryParams = {};
}

var finalQueryParams = {};
for (var i = 0, len = finalQueryParamsArray.length; i < len; ++i) {
var qp = finalQueryParamsArray[i];
finalQueryParams[qp.key] = qp.value;
if (transition && qp.visible !== false) {
transition._visibleQueryParams[qp.key] = qp.value;
}
}
return finalQueryParams;
}
Expand Down Expand Up @@ -1604,7 +1621,6 @@ define("router/utils",
"use strict";
var slice = Array.prototype.slice;


var _isArray;
if (!Array.isArray) {
_isArray = function (x) {
Expand Down
42 changes: 29 additions & 13 deletions dist/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ define("router/router",
// changed query params given that no activeTransition
// is guaranteed to have occurred.
this._changedQueryParams = queryParamChangelist.changed;
for (var k in queryParamChangelist.removed) {
if (queryParamChangelist.removed.hasOwnProperty(k)) {
this._changedQueryParams[k] = null;
}
}
trigger(this, newState.handlerInfos, true, ['queryParamsDidChange', queryParamChangelist.changed, queryParamChangelist.all, queryParamChangelist.removed]);
this._changedQueryParams = null;

Expand All @@ -354,14 +359,15 @@ define("router/router",
} else {
// Running queryParamsDidChange didn't change anything.
// Just update query params and be on our way.
oldState.queryParams = finalizeQueryParamChange(this, newState.handlerInfos, newState.queryParams);

// We have to return a noop transition that will
// perform a URL update at the end. This gives
// the user the ability to set the url update
// method (default is replaceState).
newTransition = new Transition(this);
newTransition.urlMethod = 'replace';

oldState.queryParams = finalizeQueryParamChange(this, newState.handlerInfos, newState.queryParams, newTransition);

newTransition.promise = newTransition.promise.then(function(result) {
updateURL(newTransition, oldState, true);
if (router.didTransition) {
Expand Down Expand Up @@ -449,7 +455,7 @@ define("router/router",
var args = slice.call(arguments);
if (url.charAt(0) !== '/') { args[0] = '/' + url; }

return doTransition(this, args).method('replaceQuery');
return doTransition(this, args).method(null);
},

/**
Expand Down Expand Up @@ -705,7 +711,7 @@ define("router/router",
throw e;
}

router.state.queryParams = finalizeQueryParamChange(router, currentHandlerInfos, newState.queryParams);
router.state.queryParams = finalizeQueryParamChange(router, currentHandlerInfos, newState.queryParams, transition);
}


Expand Down Expand Up @@ -839,14 +845,10 @@ define("router/router",
}

if (urlMethod) {
params.queryParams = state.queryParams;
params.queryParams = transition._visibleQueryParams || state.queryParams;
var url = router.recognizer.generate(handlerName, params);

if (urlMethod === 'replaceQuery') {
if (url !== inputUrl) {
router.replaceURL(url);
}
} else if (urlMethod === 'replace') {
if (urlMethod === 'replace') {
router.replaceURL(url);
} else {
router.updateURL(url);
Expand Down Expand Up @@ -972,21 +974,36 @@ define("router/router",
return true;
}

function finalizeQueryParamChange(router, resolvedHandlers, newQueryParams) {
function finalizeQueryParamChange(router, resolvedHandlers, newQueryParams, transition) {
// We fire a finalizeQueryParamChange event which
// gives the new route hierarchy a chance to tell
// us which query params it's consuming and what
// their final values are. If a query param is
// no longer consumed in the final route hierarchy,
// its serialized segment will be removed
// from the URL.

for (var k in newQueryParams) {
if (newQueryParams.hasOwnProperty(k) &&
newQueryParams[k] === null) {
delete newQueryParams[k];
}
}

var finalQueryParamsArray = [];
trigger(router, resolvedHandlers, true, ['finalizeQueryParamChange', newQueryParams, finalQueryParamsArray]);
trigger(router, resolvedHandlers, true, ['finalizeQueryParamChange', newQueryParams, finalQueryParamsArray, transition]);

if (transition) {
transition._visibleQueryParams = {};
}

var finalQueryParams = {};
for (var i = 0, len = finalQueryParamsArray.length; i < len; ++i) {
var qp = finalQueryParamsArray[i];
finalQueryParams[qp.key] = qp.value;
if (transition && qp.visible !== false) {
transition._visibleQueryParams[qp.key] = qp.value;
}
}
return finalQueryParams;
}
Expand Down Expand Up @@ -1658,7 +1675,6 @@ define("router/utils",
"use strict";
var slice = Array.prototype.slice;


var _isArray;
if (!Array.isArray) {
_isArray = function (x) {
Expand Down
2 changes: 1 addition & 1 deletion dist/router.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 21183ac

Please sign in to comment.