← Back to team overview

dhis2-devs-core team mailing list archive

Re: D2 app init

 

Yes, if it was a simple jquery app, this would work fine. The problem is
that for angular apps, you probably want to configure your providers using
some kind of constants (storageProvider.setBaseUrl comes to mind).

While the order of module.config order are defined by your dependency list,
they don't support promises. There are of course other ways of doing this,
but it seems like the best solution for now.

--
Morten

On Mon, Oct 6, 2014 at 2:23 PM, Jan Henrik Øverland <
janhenrik.overland@xxxxxxxxx> wrote:

> For my apps I just made a small request manager to which I just add the
> requests I want it to execute. Whatever that must be loaded sequentially is
> loaded sequentially first (manifest, system info etc) - then the rest is
> loaded simultaneously. When all the requests have called back I initiate
> the app.
>
> Could probably do something similar with d2? Add the api request configs
> to d2, then add the app itself as a callback function that receives the api
> stuff as a parameter. Then run the whole thing.
>
>
>
>
>
> On Mon, Oct 6, 2014 at 5:59 AM, Morten Olav Hansen <mortenoh@xxxxxxxxx>
> wrote:
>
>> Yeah, my thinking is that the first component of our D2JS library would
>> be a d2js bootstrapper, that would not be required, but would fetch basic
>> information from the server (manifest file, /api/me and /api/system/info
>> comes to mind). This would then be used to pre-configure our services etc,
>> making it really easy to get started using the D2JS APIs.
>>
>> I agree its not ideal, but it was the best solution i found, for when you
>> want to have async constants available for the configure part of your app.
>>
>> --
>> Morten
>>
>> On Mon, Oct 6, 2014 at 1:46 AM, Mark Polak <markpo@xxxxxxxxxx> wrote:
>>
>>> 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
>>>
>>>
>>>
>>
>> --
>> 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
>>
>>
>

References