← Back to team overview

dhis2-devs-core team mailing list archive

Re: D2 app init

 

Something like this is pretty much the only way to properly do this the “Angular” way i’m afraid or you would have to do a similar thing in the “app” controller itself and do a $q thing there or in some sort of service. But both of those are not ideal and don’t make the controllers look very clean.

Perhaps it’s a good idea to provide this with the D2JS module in some sort of way that lets you specify what you want to be “preloaded” and it’ll create an injectable object for each of those.

Kind regards,

Mark Polak
mark@xxxxxxxxxxxxxxx
markpo@xxxxxxxxxx
+47 970 36 752

On 04 Oct 2014, at 16:17, Morten Olav Hansen <mortenoh@xxxxxxxxx> wrote:

> 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
> 
> 
> 
> -- 
> Mailing list: https://launchpad.net/~dhis2-devs-core
> Post to     : dhis2-devs-core@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~dhis2-devs-core
> More help   : https://help.launchpad.net/ListHelp


Follow ups

References