running code after angular bootstrap

158 views
Skip to first unread message

jérémie

unread,
Feb 25, 2013, 5:38:09 PM2/25/13
to ang...@googlegroups.com
Hi folks,

I'm wondering about how to run post initialization code with angular. Here's my use case, I have a controller and a service, my service is performing some background task fetching data remotely and I want my controller to be alerted each time my service has received some data according to an observer pattern. Assuming my controller and my service are declared and fully ready to work, I'd like to run this kind of instructions:

// my services are here
angular.module('myApp.services', []);

// my controllers are here
angular.module('myApp.controllers', []);

// I'd like to do this
angular.module('myApp', ['myApp.services','myApp.controllers']).run(function(service, controller) {

        service.subscribe(controller);
        service.start();

        // some other initialization code
});

   
Do you guys have any idea how to deal with that properly and if it is the good approach ? 
Thx in advance for the answer and all the support brought by these google group pages


Alexander Burakevych

unread,
Feb 25, 2013, 6:00:17 PM2/25/13
to ang...@googlegroups.com
Do you start you start your service from the controller? If so, you can receive response (success or failure) and act depending on it.
In my application I'm doing the following: NetworkStateVersionService run continuously from the controller to fetch the versions of different components on the back-end and if any of the versions has changed - run the update service:
...
        NetworkStateVersionService.get({}, function(data) {
            var isInSync = true;
           
            for (var key in data) {
                if ($rootScope.networkStateVersion[key] !== data[key]) {
                    isInSync = false;
                    $rootScope.networkStateVersion = data;
                    break;
                }
            }
           
            if (!isInSync) {
                console.log("About to start FULL UPDATE...");
                // Run another service here...
            }
...

jérémie

unread,
Feb 25, 2013, 6:27:17 PM2/25/13
to ang...@googlegroups.com
Hi Alexander and thanks for your answer,

I was starting my "background" service from my controller at the beginning but decided it was not really clean. I don't want my controller to manage this kind of service because it's a service that could be shared among different controllers or services. Now I'm doing the following but i'm not completely satisfied as I'm not successful in injecting controller into my main app module.

// my services are here
angular.module('myApp.services', []).service('myService', function() { 

  // services methods and variables..

  // pattern relative method
  this.register = function(observer) {
    observers.add(observer);
  }

});

// my controllers are here
angular.module('myApp.controllers', ['myApp.services']).controller('myController', function(myService){

  // I build my controller here...

  // and I register it to the background service
  // I WANT TO THIS IN THE MAIN MODULE
  myService.register(this);
});


// main module
var myApp = angular.module('myApp',['ngSanitize','myApp.controllers','myApp.services']).run(function(service){
    
    schedulerService.start();

});

Malik Junaid

unread,
Feb 25, 2013, 7:01:00 PM2/25/13
to ang...@googlegroups.com

Hi Alex, Trying to understand why you are using the observer pattern. Are you invoking certain functions on controller from service when a task is complete ? can you not just use the events ?
Reply all
Reply to author
Forward
0 new messages