From 495a02c3cbde501c1c149bce137806669209bc29 Mon Sep 17 00:00:00 2001 From: christopherthielen Date: Tue, 19 May 2015 07:51:58 -0500 Subject: [PATCH] fix(urlMatcherFactory): Revert to 0.2.13 behavior where all string parameters are considered optional fix(urlRouter): When reloadOnSearch is false, also update the URL. Fixes #1963 Fixes #1803 Fixes #1079 --- src/state.js | 8 +++++++- src/urlMatcherFactory.js | 7 +++---- test/stateSpec.js | 13 +++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/state.js b/src/state.js index 7bc0a754e..56ee5a055 100644 --- a/src/state.js +++ b/src/state.js @@ -1032,9 +1032,15 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) { // that we've initiated ourselves, because we might accidentally abort a legitimate // transition initiated from code? if (shouldSkipReload(to, toParams, from, fromParams, locals, options)) { - if (to.self.reloadOnSearch !== false) $urlRouter.update(); + if (hash) toParams['#'] = hash; $state.params = toParams; copy($state.params, $stateParams); + if (options.location && to.navigable && to.navigable.url) { + $urlRouter.push(to.navigable.url, toParams, { + $$avoidResync: true, replace: options.location === 'replace' + }); + $urlRouter.update(true); + } $state.transition = null; return $q.when($state.current); } diff --git a/src/urlMatcherFactory.js b/src/urlMatcherFactory.js index fd4a6ea9b..bf116f027 100644 --- a/src/urlMatcherFactory.js +++ b/src/urlMatcherFactory.js @@ -571,15 +571,14 @@ function $UrlMatcherFactory() { function valToString(val) { return val != null ? val.toString().replace(/\//g, "%2F") : val; } function valFromString(val) { return val != null ? val.toString().replace(/%2F/g, "/") : val; } -// TODO: in 1.0, make string .is() return false if value is undefined by default. -// function regexpMatches(val) { /*jshint validthis:true */ return isDefined(val) && this.pattern.test(val); } - function regexpMatches(val) { /*jshint validthis:true */ return this.pattern.test(val); } var $types = {}, enqueue = true, typeQueue = [], injector, defaultTypes = { string: { encode: valToString, decode: valFromString, - is: function(val) { return typeof val === "string"; }, + // TODO: in 1.0, make string .is() return false if value is undefined/null by default. + // In 0.2.x, string params are optional by default for backwards compat + is: function(val) { return val == null || !isDefined(val) || typeof val === "string"; }, pattern: /[^/]*/ }, int: { diff --git a/test/stateSpec.js b/test/stateSpec.js index 6517fdd6e..8acd8ece2 100644 --- a/test/stateSpec.js +++ b/test/stateSpec.js @@ -229,6 +229,19 @@ describe('state', function () { expect(called).toBeFalsy(); })); + it('updates URL when changing only query params via $state.go() when reloadOnSearch=false', inject(function ($state, $stateParams, $q, $location, $rootScope){ + initStateTo(RS); + var called; + $state.go(".", { term: 'goodbye' }); + $rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) { + called = true + }); + $q.flush(); + expect($stateParams).toEqual({term: 'goodbye'}); + expect($location.url()).toEqual("/search?term=goodbye"); + expect(called).toBeFalsy(); + })); + it('does trigger state change for path params even if reloadOnSearch is false', inject(function ($state, $q, $location, $rootScope){ initStateTo(RSP, { doReload: 'foo' }); expect($state.params.doReload).toEqual('foo');