From 82783374ae07b7b7f07b597a712fbc89a00ca457 Mon Sep 17 00:00:00 2001 From: christopherthielen Date: Sat, 31 Jan 2015 15:43:01 -0600 Subject: [PATCH] fix(sticky): fixed reload: true for ui-router 0.2.8 - in 0.2.8, params and ownParams were arrays of strings. ( Additional fix for #139 ) --- src/sticky.js | 28 ++++++++++++++++++---------- src/stickyProvider.js | 2 +- test/stickySpec.js | 4 ++-- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/sticky.js b/src/sticky.js index 28ca581..e029b0f 100644 --- a/src/sticky.js +++ b/src/sticky.js @@ -272,8 +272,8 @@ angular.module("ct.ui.router.extras.sticky").config( savedFromStatePath = fromState.path; // Try to resolve options.reload to a state. If so, we'll reload only up to the given state. - var reload = options && options.reload; - var reloadStateTree = (reload === true ? savedToStatePath[0].self : $state.get(reload, rel)); + var reload = options && options.reload || false; + var reloadStateTree = reload && (reload === true ? savedToStatePath[0].self : $state.get(reload, rel)); // If options.reload is a string or a state, we want to handle reload ourselves and not // let ui-router reload the entire toPath. if (options && reload && reload !== true) @@ -295,17 +295,25 @@ angular.module("ct.ui.router.extras.sticky").config( // being reloaded, and add a param value to the transition. This will cause the "has params changed // for state" check to return false, and the states will be reloaded. if (reloadStateTree) { + currentTransition.toParams.$$uirouterextrasreload = Math.random(); var params = reloadStateTree.$$state().params; var ownParams = reloadStateTree.$$state().ownParams; - var tempParam = new $urlMatcherFactoryProvider.Param('$$uirouterextrasreload'); - params.$$uirouterextrasreload = ownParams.$$uirouterextrasreload = tempParam; - currentTransition.toParams.$$uirouterextrasreload = Math.random(); - - restore.restoreFunctions.push(function() { - delete params.$$uirouterextrasreload; - delete ownParams.$$uirouterextrasreload; - }); + if (versionHeuristics.hasParamSet) { + var tempParam = new $urlMatcherFactoryProvider.Param('$$uirouterextrasreload'); + params.$$uirouterextrasreload = ownParams.$$uirouterextrasreload = tempParam; + restore.restoreFunctions.push(function() { + delete params.$$uirouterextrasreload; + delete ownParams.$$uirouterextrasreload; + }); + } else { + params.push('$$uirouterextrasreload'); + ownParams.push('$$uirouterextrasreload'); + restore.restoreFunctions.push(function() { + params.length = params.length -1; + ownParams.length = ownParams.length -1; + }); + } } // $StickyStateProvider.processTransition analyzes the states involved in the pending transition. It diff --git a/src/stickyProvider.js b/src/stickyProvider.js index dcf337b..4b36b93 100644 --- a/src/stickyProvider.js +++ b/src/stickyProvider.js @@ -97,7 +97,7 @@ function $StickyStateProvider($stateProvider) { // Duplicates logic in $state.transitionTo, primarily to find the pivot state (i.e., the "keep" value) function equalForKeys(a, b, keys) { - if (angular.isObject(keys)) { + if (!angular.isArray(keys) && angular.isObject(keys)) { keys = protoKeys(keys, ["$$keys", "$$values", "$$equals", "$$validates", "$$new", "$$parent"]); } if (!keys) { diff --git a/test/stickySpec.js b/test/stickySpec.js index 95fb8f1..78e2490 100644 --- a/test/stickySpec.js +++ b/test/stickySpec.js @@ -239,7 +239,7 @@ describe('stickyState', function () { return { 'main': {}, 'main.other': { sticky: true, views: { 'other@main': {} } }, - 'main.product': { sticky: true, views: { 'product@main': {} }, params: { 'product_id': 15 } }, + 'main.product': { sticky: true, views: { 'product@main': {} }, url: '/:product_id' }, 'main.product.something': {} }; } @@ -404,7 +404,7 @@ describe('stickyState', function () { testGo('A._1', { entered: ['A', 'A._1' ] }); testGo('A._2', { inactivated: [ 'A._1' ], entered: 'A._2' }); testGo('A._1', { reactivated: 'A._1', inactivated: 'A._2' }); - resetTransitionLog(); +// resetTransitionLog(); testGo('A._2', { exited: [ 'A._1', 'A._2', 'A' ], entered: [ 'A', 'A._2' ] }, { reload: true }); }); });