0

I have a problem pretty much similar than the issue exposed in this question (AngularJS : Initialize service with asynchronous data). My difference is that I'm using angularAMD to load scripts asynchronously and I just can't fit the approaches above in the following app.config:

define(['angularAMD'], function (angularAMD) {
   var app = angular.module('app', []);
   app.config(['$routeProvider', function ($routeProvider) {
     // some code here
   }]);
   app.run(['srvRules', function (srvRules) {
     srvRules.getRules().then(function (result) {
        // I need to bootstrap only after I get the rules from the server
      });
   }]);
   return angularAMD.bootstrap(app);
});

EDIT 1

This is the approach I'm trying to accomplish: https://stackoverflow.com/a/21189057/2407203

EDIT 2

Adding more information: I have a webapi as the backend, it is a legacy system which provides all kinds of business rules needed to set up the application. The main information provided at this moment are:

  1. I use $translateProvider(http://angular-translate.github.io) to dinamically load the dictionary with the current culture. All the texts should be taken from this dictionary.
  2. I have a service that monitors all route changes and allows or restricts the access based on the rules returned.
  3. Some information should be placed on the screen based on the rules. Those information are all encapsuled inside a directive on the initial html.
  4. Some html controls may appear or not based on the rules as well.
Community
  • 1
  • 1
Norbor Illig
  • 658
  • 6
  • 14
  • What approach are you referring to? The select answer to the linked question suggest the use of resolve attribute of `$routeProvider` but that does not seems to be what you are doing. Are you referring to the "Manual Bootstrap" section? – marcoseu Jan 30 '15 at 12:35
  • Sorry @marcoseu, this approach (http://stackoverflow.com/a/21189057/2407203). I need the async data for some services, not only controllers. If I only needed it in the controllers the resolve option would be fine. – Norbor Illig Jan 30 '15 at 16:57
  • Can you share a little more information about what is returned by your `getRules()`? Are you calling a simple JSON file stored in a remote server? Can you dictate what `getRules()` returns? The solution could probably be implemented using requirejs' `text` filter or angularAMD's `ngload` filter. – marcoseu Jan 31 '15 at 19:01
  • @marcoseu, Sure thing, I'm editing the post with the info requested – Norbor Illig Feb 02 '15 at 13:01

1 Answers1

0

There's really no easy or elegant way out. I ended up doing the following:

  1. Started using ui-router(https://github.com/angular-ui/ui-router) instead of ngRoute;
  2. Created a root abstract state, parent of all other states;
  3. Used the resolve section of the root state to load all data needed before any controller get instantiated
Norbor Illig
  • 658
  • 6
  • 14