javascript - UI-Router States: How to make app.run with $rootScope.$on minification safe -
i'm using ui-router in app , check if order of states correct, use code this:
.value('myroutesteps', [ { uisref: 'myroute.home', valid: true }, { uisref: 'myroute.pageone', valid: false }, { uisref: 'myroute.pagetwo', valid: false }, ]) .run([ '$rootscope', '$state', 'myroutesteps', function ($rootscope, $state, myroutesteps) { $rootscope.$on('$statechangestart', function (event, tostate, toparams, fromstate, fromparams) { var cangotostep = false; var tostateindex = _.findindex(myroutesteps, function (myroutestep) { return myroutestep.uisref === tostate.name; }); console.log('tostateindex', tostateindex) if (tostateindex === 0) { cangotostep = true; } else { cangotostep = myroutesteps[tostateindex - 1].valid; } console.log('cangotostep', tostate.name, cangotostep); // stop state changing if previous state invalid if (!cangotostep) { // abort going step event.preventdefault(); } }); } ]);
i'm getting error on line $rootscope.$on('$statechangestart', function (event, tostate, toparams, fromstate, fromparams) {
if minification.
so i've changed $rootscope.$on('$statechangestart', ['event','tostate','toparams','fromstate','fromparams', function (event, tostate, toparams, fromstate, fromparams) {
yet i'm still getting error.
update:
when @ minified code see this:
}]).run(["$rootscope", "$state", "ordersteps", function (n, t, i) { n.$on("$statechangestart", function (n, t) { var r = !1, u = _.findindex(i, function (n) { return n.uisref === t.name
isn't right guess...how fix? suggestions?
_.findindex
can return -1
in case not found. 0
first item in array, -1
not found. cause problems here
if (tostateindex === 0) { cangotostep = true; } else { cangotostep = myroutesteps[tostateindex - 1].valid; }
when tostateindex
-1
index of out range exception.
you want this:
if (tostateindex < 0) { cangotostep = true; } else { cangotostep = myroutesteps[tostateindex].valid; }
to explain how happen... when first load page, ui router go ''
state, when tries go ''
triggers $statechangestart
. _.findindex
cannot find ''
in myroutesteps
, tostateindex
becomes -1
. -1
not equal 0
falls in else
block, -1 - 1
-2
, myroutesteps[-2]
out of range.