1

I have use case where i need to execute certain javascript function to refresh members data, that means i will have to call one function when pusher notification arrives.

So basically i have one service where i created array of callbacks and two functions, one to register callback and one to execute it.

And it looks like this:

In shared service i have this:

var callbacks = [];

function triggerCallback() {
    angular.forEach(callbacks, function(callback) {
        callback(member);
    });
}

function registerCallback(callback) {
    callbacks.push(callback);
}

and in one of my components when member enters that component i register function callback like this:

code in component looks like this:

$onInit() {
 functionToGetSomethingFromApi(param1, param2);
 sharedService.registerCallback(functionToGetSomethingFromApi);
}

So here when member actually visit page where i execute this code i register callback, and next time when this page is visited i do not make again same api call, since i don't want to make unnecessary calls to API, BUT when pusher notification arrives, i want to execute this code.

So in my pusher service i have this code (which actually works)

pusher.on('notify', function (notification) {
    sharedService.triggerCallback();
});

So my question here is, what is best way to pass those two params from component to service and actually to use those when triggering callback from some location ?

I know few ways how to store them, i can save callback and params, and then somehow execute it, but i was wondering what is best way to do it...

Hopefully my question is clear and thanks in advance for answers!

User3250
  • 2,961
  • 5
  • 29
  • 61
Sahbaz
  • 1,242
  • 4
  • 17
  • 39

0 Answers0