Hi,

I'm also looking for an elegant solution for this.
In my case, I need to load some configuration variables from the backend 
before some services can be used.

I am aware of the resolve functionality in routeProvider.
But as a few people have said here, there must be a more elegant solution 
than attaching the same resolve function to every route.

Witold, an example would be as follows. Let's say in a controller there's

var someDefaultValue = service1.getDefaultValue();
service2.foo(someDefaultValue);

where the defaultValue here has to be retrieved from the backend and is 
used in every controller.
As far as I know, the only ways to guarantee that defaultValue is ready is 
either to bootstrap the app manually or pass it to the controller via 
routeProvider's resolve (which means it has to be done in every route).
Backing defaultValue with a promise would have worked if it were to be used 
in $scope.
However, defaultValue is used in another service (service2), which doesn't 
wait for service1 to have defaultValue initialized.

On Monday, 27 January 2014 21:34:41 UTC, Witold Szczerba wrote:
>
> Why would you need to add resolve to every controller? The solution 
> described by me works pretty well, and I have never add any resolve to 
> controllers... 
>
> 2014-01-27 Cameron MacKenzie <[email protected] <javascript:>>: 
> > Is there still no good solution for this? 
> > 
> > It'd be really nice if all of the dependencies could be resolved in 
> run() 
> > before the rest of the application executes. As it stands, you have to 
> add a 
> > resolve to EVERY controller for the dependencies you need. That seems 
> like a 
> > maintenance pain in the butt. Is there any other solution? 
> > 
> > 
> > On Friday, September 27, 2013 3:03:56 AM UTC-4, Witold Szczerba wrote: 
> >> 
> >> So your case is the same as my. Promises work for me very well in this 
> >> scenario. Directives work very well with them as well. 
> >> Rember that you cam make entire app invisible or hidden behind some 
> fancy 
> >> "please wait" banner until every crucial data arrives (look at the API, 
> you 
> >> can resolve multiple promises in one go), so there is no need to worry 
> how 
> >> does your directives looks like when data is not yet available. 
> >> 
> >> Regards, 
> >> Witold Szczerba 
> >> --- 
> >> Sent from my mobile phone. 
> >> 
> >> On Sep 27, 2013 5:00 AM, "Tim Hardy" <[email protected]> wrote: 
> >>> 
> >>> FeatureToggles.  Is a feature turned on or off?  If it's off, then 
> >>> nothing related to it should be shown - buttons, tabs, grids, 
> anything. 
> >>> Directives are an elegant way to hangle this, just place a 
> >>> data-feature="SomeFancyFeature" on related elements. 
> >>> 
> >>> Callbacks don't work well with directives.  Directives don't wait. 
>  They 
> >>> bind, and if the data they need isn't available when they bind, then 
> you 
> >>> have to make a choice.  Do you show the element and hide it later?  Do 
> you 
> >>> hide it and show it later?  It's so much easier to simply accept that 
> in 
> >>> scenarios, there really is data that is so important that some things 
> need 
> >>> to wait for it.  It's simply not worth it to let the controller and 
> view 
> >>> partially "do" anything. 
> >>> 
> >>> On Thursday, September 26, 2013 4:04:48 PM UTC-5, Witold Szczerba 
> wrote: 
> >>>> 
> >>>> What is your use case for this? 
> >>>> I had similar problem, I mean, back then I though I had, with my 
> >>>> authorization system. 
> >>>> I had to fetch data from server to be able to figure out what is the 
> >>>> user allowed to see, like what options in menu and what actions were 
> >>>> possible to they. 
> >>>> 
> >>>> The solution was to back everything with promises. Promises are very 
> >>>> nice, they let you do things at once and there is no need to wait for 
> a 
> >>>> service to return data before the consumers of the service run. 
> >>>> 
> >>>> So, the service triggers the http request and immediately return 
> >>>> promise. If your controllers need the response, they ask the promise 
> to 
> >>>> resolve with callback. There is no need for the controllers to 
> actually be 
> >>>> postponed. They will initialize, use the promise returned by the 
> service and 
> >>>> once the data are available - they continue. 
> >>>> If the data is available before controller was initialized, the 
> promise 
> >>>> will trigger callback immediately. If not, the callbacks will wait. 
> It 
> >>>> worked for me. 
> >>>> 
> >>>> Regards, 
> >>>> Witold Szczerba 
> >>>> --- 
> >>>> Sent from my mobile phone. 
> >>>> 
> >>>> On Dec 24, 2012 1:37 PM, "Sergey Chico" <[email protected]> wrote: 
> >>>>> 
> >>>>> I don't think jsfiddle or plunkr will be useful. I need something 
> like 
> >>>>>     app.run(['$rootScope', '$http', function($rootScope, $http) { 
> >>>>>         $http.get('some/url').success(function (data) { 
> >>>>>             // make the rest of application init (e.g. controllers 
> can 
> >>>>> use the success function data) 
> >>>>>             // for example 
> >>>>> 
> >>>>> $rootScope.veryImportantDataApplicationNeedsOrElseItMustNotInit = 
> data; 
> >>>>>             // then make controllers init so they can use 
> >>>>> 
> $rootScope.veryImportantDataApplicationNeedsOrElseItMustNotInit.someProperty 
>
> >>>>>         }); 
> >>>>>     }]); 
> >>>>> 
> >>>>> Now I write code this way: 
> >>>>>         routeProvider.when('/path1, { 
> >>>>>             templateUrl: '/partials/path1.html', 
> >>>>>             controller: 'path1Controller', 
> >>>>>             resolve: // includes function returning $http promise 
> with 
> >>>>> veryImportantDataApplicationNeedsOrElseItMustNotInit 
> >>>>>         }); 
> >>>>>         routeProvider.when('/path2, { 
> >>>>>             templateUrl: '/partials/path2.html', 
> >>>>>             controller: 'path2Controller', 
> >>>>>             resolve: // includes function returning $http promise 
> with 
> >>>>> veryImportantDataApplicationNeedsOrElseItMustNotInit 
> >>>>>         }); 
> >>>>> ............................. 
> >>>>> ............................. 
> >>>>>         routeProvider.when('/path1000000000000, { 
> >>>>>             templateUrl: '/partials/path1000000000000.html', 
> >>>>>             controller: 'path1000000000000Controller', 
> >>>>>             resolve: // includes function returning $http promise 
> with 
> >>>>> veryImportantDataApplicationNeedsOrElseItMustNotInit 
> >>>>>         }); 
> >>>>> ...and there are 2 problems: 
> >>>>> 1. I repeat myself every of 1000000000000 paths for routeProvider 
> >>>>> 2. Controllers which are directly in index.html (using 
> ng-controller) 
> >>>>> still can not use someProperty of 
> >>>>> veryImportantDataApplicationNeedsOrElseItMustNotInit or I need to 
> place 
> >>>>> promise.then(...) into them 
> >>>>> So that looks VERY ugly and I just want to know is there any other 
> ways 
> >>>>> to preload the data for my application. 
> >>>>> 
> >>>>> 
> >>>>> понедельник, 24 декабря 2012 г., 15:57:30 UTC+4 пользователь Pawel 
> >>>>> Kozlowski написал: 
> >>>>>> 
> >>>>>> Use the .run() block of a module. 
> >>>>>> Send a jsfiddle or plunk if you need help wit the code. 
> >>>>>> 
> >>>>>> Cheers, 
> >>>>>> Pawel 
> >>>>>> 
> >>>>>> On Monday, December 24, 2012, Sergey Chico wrote: 
> >>>>>>> 
> >>>>>>> I need to load some data from server before all controllers init. 
> Now 
> >>>>>>> I use the resolve of $routeProvider. But this way I need to 
> include my 
> >>>>>>> resolve function returning promise into every resolve for every 
> route. This 
> >>>>>>> looks not DRY-way. Any thoughts? 
> >>>>>>> 
> >>>>>>> -- 
> >>>>>>> You received this message because you are subscribed to the Google 
> >>>>>>> Groups "AngularJS" group. 
> >>>>>>> To post to this group, send email to 
> >>>>>>> [email protected]<javascript:>. 
>
> >>>>>>> To unsubscribe from this group, send email to 
> >>>>>>> [email protected] <javascript:>. 
> >>>>>>> Visit this group at 
> http://groups.google.com/group/angular?hl=en-US. 
> >>>>>>> 
> >>>>>>> 
> >>>>>> 
> >>>>>> 
> >>>>>> 
> >>>>>> -- 
> >>>>>> Question? Send a fiddle 
> >>>>>> (http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk 
> >>>>>> (http://plnkr.co/) 
> >>>>>> Need help with jsFiddle? Check this: 
> >>>>>> 
> http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/ 
> >>>>>> 
> >>>>>> Looking for UI widget library for AngularJS? Here you go: 
> >>>>>> http://angular-ui.github.com/ 
> >>>>> 
> >>>>> -- 
> >>>>> You received this message because you are subscribed to the Google 
> >>>>> Groups "AngularJS" group. 
> >>>>> To post to this group, send email to [email protected]. 
> >>>>> To unsubscribe from this group, send email to 
> >>>>> [email protected]. 
> >>>>> Visit this group at http://groups.google.com/group/angular?hl=en-US. 
>
> >>>>> 
> >>>>> 
> >>> 
> >>> -- 
> >>> You received this message because you are subscribed to the Google 
> Groups 
> >>> "AngularJS" group. 
> >>> To unsubscribe from this group and stop receiving emails from it, send 
> an 
> >>> email to [email protected]. 
> >>> 
> >>> To post to this group, send email to [email protected]. 
> >>> Visit this group at http://groups.google.com/group/angular. 
> >>> For more options, visit https://groups.google.com/groups/opt_out. 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "AngularJS" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an 
> > email to [email protected] <javascript:>. 
> > To post to this group, send email to [email protected]<javascript:>. 
>
> > Visit this group at http://groups.google.com/group/angular. 
> > For more options, visit https://groups.google.com/groups/opt_out. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to