← Back to team overview

dhis2-devs-core team mailing list archive

Re: D2 app init

 

Just to build on that, you can use $q.all to wait for all requests to
finish.. like this:

"use strict";
var bootstrap = function( module, constant, urls ) {
    var injector = angular.injector(['ng']);
    var $q = injector.get('$q');
    var $http = injector.get('$http');

    var requests = [];

    angular.forEach(urls, function( url ) {
        requests.push($http.get(url));
    });

    $q.all(requests).then(function( arr ) {
        var config = {};

        angular.forEach(arr, function( item ) {
            angular.extend(config, item.data);
        });

        angular.module('config', []).constant(constant, config);
        angular.element(document.body).ready(function() {
            angular.bootstrap(document.body, [module]);
        });
    });
};

bootstrap('MyApp', 'CONFIG', ['system1.json', 'system2.json']);

​

This will of course extend it all to one object, which might not be what
you are looking for.. but it could be a good start.

How are people dealing with this in their apps? even the manifest file must
somehow be fetched and be globally available (how would you else configure
your storage providers etc?).

--
Morten

On Sat, Oct 4, 2014 at 8:25 PM, Morten Olav Hansen <mortenoh@xxxxxxxxx>
wrote:

> Hi everyone
>
> Just wanted to quickly share to link [1]. It shows an example off how to
> run one or more async request before your main module is initialized. This
> is very useful in d2 app development, since you probably want to get
> /api/system/info and /api/me etc and provide them as constants in your app.
>
> A simple example would be:
> (function() {
>     angular.injector(['ng']).get('$http').get('system.json').then(
>         function( response ) {
>             angular.module('config', []).constant('CONFIG', response.data);
>             angular.element(document.body).ready(function() {
>                 angular.bootstrap(document.body, ['MyApp']);
>             });
>         }
>     );
> })();
>
> Of course, if you have multiple links you want to get, you probably want
> to use the injector to also get $q service etc, to properly send out async
> request and pull them back together before doing module init.
>
> At some point, we should probably provide our own d2.init module, which
> would allow this kind of bootstrapping (also providing Me service etc).
>
> [1]
> http://stackoverflow.com/questions/16286605/initialize-angularjs-service-with-asynchronous-data
>
> --
> Morten
>
>
>

Follow ups

References