I wonder if I use $timeout for broadcasting event from $parentCtrl to $childCtrl sometime not work

484 views
Skip to first unread message

Hoàng Ngọc Dũng

unread,
Nov 30, 2014, 11:06:35 PM11/30/14
to ang...@googlegroups.com
From the reference here for using $broadcast to avoid the child controller isn't initialized yet.

http://stackoverflow.com/questions/22075044/angular-broadcast-not-working
http://stackoverflow.com/questions/22327855/emit-from-child-controller-works-but-broadcast-from-parent-scope-doesnt

I wonder if any problems for that because we don't know exactly when the children finish initializing or not.
Is there any best way for that ?
I have a preload data for parent controller and I need to broadcast this data to all children controllers, I know we can use $scope.$parent to get it directly but I want to avoid the children need to know about '$parent'.

Thanks !

Sander Elias

unread,
Nov 30, 2014, 11:20:17 PM11/30/14
to ang...@googlegroups.com
Hi Hoàng,

You don't need to broadcast data to children. If you put data on the scope like this: `$sope.config={hello:'world'}` you can just use it anywhere down the inheritance tree.
However, while this will work, it's not something you should do without thinking about it. Often the better solution is to put the data inside an service, and share that service where you need it.
I will build you a small example, Be back in a little while!

Regards
Sander 

Sander Elias

unread,
Nov 30, 2014, 11:53:08 PM11/30/14
to ang...@googlegroups.com
Hi,,


Regards
Sander

Hoàng Ngọc Dũng

unread,
Dec 1, 2014, 1:29:08 AM12/1/14
to ang...@googlegroups.com
Thanks so much. I will try to use that for best practices.
But i am from an outsource industry, the customer they are familiar with publish/subscribe patterns. They want to communicate between controllers via events

ParentController - subscribe 'ctr1Submitted' -> publish 'ctrl1Updated'
   Controller-1 - fire event 'ctrl1Submitted', ctrl1.data
   Controller-2 - subscribe 'ctrl1Updated' to update data

This flow is work well with create mode. But in edit mode, We already has data, the customer want to fire events in ParentController
ParentController($scope, data) {
   $scope.$broadcast('ctrl1Updated', data)
}
unfortunately the Controller-2 is not initialized yet to subscribe this event, I try to use
$timeout(function() {
  $scope.$broadcast('ctrl1Updated', data)
}) ;
It works well, but the customer is afraid of $timeout sometimes does not work.
Just want to know and learn more about that problems. Using Service is great, but in my situation I need to make sure about events. Please share your idea here. Thanks !

Sander Elias

unread,
Dec 1, 2014, 1:59:41 AM12/1/14
to ang...@googlegroups.com
Hoàng, 

I prefer to stay clear of events if possible. If your customer wants to make sure the events are processed, you need some form of acknowledging that the events are received. That's whole well possible, but it will come with a pretty large fee.
The thing with events is, that you know you broadcast them, but you never know for sure anyone is listing. 
My educated guess is that in your case, you are broadcasting an event while angular still is compiling parts of your page. That's why the timeout works.

Regards
Sander
 

Al J

unread,
Dec 2, 2014, 6:46:18 AM12/2/14
to ang...@googlegroups.com
My educated guess is that in your case, you are broadcasting an event while angular still is compiling parts of your page.

that was _exactly_ what was happening to me -- the parents' broadcast was not being heard by the children because the children were not done instantiating yet

I wrapped a promise around a dataset to make it work, but the setTimeout was written in that promise (I couldn't make the promise work without the setTimeout because it didn't call out to anything, it was just a mass of calculations.)

In my case the setTimeout in the promise was enough to let the children finish their instantiation, I rationalized it by saying that javascript is single threaded and setTimeout (for now) is the only way to yield execution.

I wanted to wrap the  promise around the children's instantiation but could not find a list of all the .$functions that are called (or events emitted) by angular in the course of instantiation -- I was hoping to find one so that it didn't seem so coincidental that the children were done instantiating.

Maybe by next time I'll know more and can build something that feels more concrete
Reply all
Reply to author
Forward
0 new messages