AngularJS UI Router not executing unspecified resolve dependencies in order -
i'm running in problem how ui router handles order loads resolves. end goal of i'm trying accomplish having services load data , store it, , have data loaded (requested) once , accessible through service.
the current way i've gone implementing follows pattern:
in higher level state resolve, tell service load data. result of resolve not dependency of sub services, because data accessed injecting service in controller. sub state resolves may depend on service has been "primed" in previous state works great when you're clicking through application (i.e, state -> state a.b -> state a.b.c) however, breaks down when go directly nested state (state a.b or state a.b.c). problem occurs because ui router not ensure order resolves executed in if child state not specify resolve of parent state dependency. see example below:
app.config(function($stateprovider) { $stateprovider .state('a', { url: "/a", resolve: { data: function(service) { return service.getdata(); // makes network request } } }) .state('a.b', { url :"/b", resolve: { specificdata: function(service) { // if add data dependency here, works fine return service.usedatatogetspecificdata(); // doesn't make network request } } }) });
in example, if user goes directly state a.b specificdata resolve fail. service.usedatatogetspecificdata makes no network requests , accesses data stored in service. call fails because resolve of state has not finished yet because network request.
a solution have service handle case doesn't have data yet , manage waiting until has data, feels bit overblown service doing, , seems defeat point of ui router.
another solution (what i'm going with) adding "data" dependency of resolve in state a.b though isn't used. forces a.b wait on finish resolves, , works great. however, feels hack , extremely non intuitive. looking @ code without knowing scratch head why unused variable declared in a.b's resolve.
the order of resolves not being guaranteed without specifying dependency way ui router written behave, feel must doing weird / wrong.
another solution (what i'm going with) adding "data" dependency of resolve in state a.b though isn't used. forces a.b wait on finish resolves, , works great. however, feels hack , extremely non intuitive
this the common way this. add multiple sequential resolves 1 route. example, add attachuser
hook returns promise http request, isloggedin
has attachuser dependency. i've searched lot , never found better. nothing special writen case in ui-router, beyond angular's injector. if case special because of inheritance, same concern here , here.
so feel must doing weird / wrong.
you're not. it's thought weird / wrong
Comments
Post a Comment