Call from Provider back to page


#1

I can’t seem to get my head around how to do this in the newer version of Angular. In version 1 I would use $defer and $emit but Angular4 does not use those methods.

Basically I have a page which passes data to a provider, the provide will call a number of functions within itself and one of those functions will generate as a solution. When that solution is generated it should be passed the back the page that called the provider.

Can I use something like the emit call within a component? or has some has an example of a callback which is more complex than a simple http call.

Any suggestions will be much appreciated.


#2

This exists: http://ionicframework.com/docs/api/util/Events/


#3

Many thanks Sujan that was exactly what I was after.


#4

That’s not an idiomatic way to think in Angular apps. Instead, the provider should return a future (Promise or Observable) that resolves when the answer is ready.

I would not use events for this, because it needlessly breaks up what is logically a single (albeit asynchronous) operation. Which makes more sense to you:

constructor(events: Events) {
  let onEnd  = (foo) => this.frotzFinished(foo);
  events.subscribe("frotzed", onEnd);
}

startFrotz(): void {
  this.frotzer.frotz();
}

frotzFinished(foo: string) {
}

…where we have one operation spread across three totally unrelated functions, or this:

frotz(): void {
  this.frotzer.frotz().then(rslt => this.frotzed = rslt);
}

…where we can keep everything self-contained in a single one-line method?