Broadcast/emit - are they synchronous?

4,091 views
Skip to first unread message

jvarg...@gmail.com

unread,
Oct 1, 2012, 7:17:33 AM10/1/12
to ang...@googlegroups.com
Hi Everyone,

I have a nested directive structure. The navigation buttons are in the parent directive template. When the navigation is clicked  the handler in the parent should call  a "validate" method on some of the child directives. If all child directive return a success, the the parent should submit some data to the server via an injected service. Does it make sense to use broadcast and emit for implementing this?

I am not sure how to make the handler in  the parent to stop processing after it has broadcasted the "navigation" event to the interested child directives and resume only after all of them have returned a success.

Any help is appreciated.

Regards,
John

Pawel Kozlowski

unread,
Oct 1, 2012, 6:07:30 PM10/1/12
to ang...@googlegroups.com
Could you try to prepare a jsfiddle or a plunk with your use case?

It is kind of hard to help without live code as your description is a bit "abstract".

Cheers,
Pawel
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 


--
Question? Send a fiddle (http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk (http://plnkr.co/)
Need help with jsFiddle? Check this: http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go: http://angular-ui.github.com/

John

unread,
Oct 17, 2012, 2:05:48 PM10/17/12
to ang...@googlegroups.com
Restating my question:

Consider the following code snippet in a parent controller

Code A
$scope.$broadcast('myevent')
Code B

Does Code B get executed before or after all the listeners in the chid directives have processed the event.

Thanks
John

Peter Bacon Darwin

unread,
Oct 17, 2012, 3:19:54 PM10/17/12
to ang...@googlegroups.com
The $broadcast and $on are synchronous, but your handlers may be asynchronous.

--

John

unread,
Oct 18, 2012, 7:19:18 AM10/18/12
to ang...@googlegroups.com
Thanks Pete. I would appreciate if you could explain a bit more "but your handlers may be asynchronous."

Regards,
Jon

 

On Wednesday, October 17, 2012 3:20:19 PM UTC-4, Peter Bacon Darwin wrote:
The $broadcast and $on are synchronous, but your handlers may be asynchronous.

On 17 October 2012 19:05, John <pilo...@gmail.com> wrote:
Restating my question:

Consider the following code snippet in a parent controller

Code A
$scope.$broadcast('myevent')
Code B

Does Code B get executed before or after all the listeners in the chid directives have processed the event.

Thanks
John

Peter Bacon Darwin

unread,
Oct 18, 2012, 7:49:11 AM10/18/12
to ang...@googlegroups.com
If I do something like this:

$rootScope.$on('someEvent', function() {
  $timeout(function() {
    // Do some stuff
  });
});

Then the "Do some stuff" will be done asynchronously and the event broadcast will finish before this stuff is done.  A classic case would be calling a $http request in an event handler.

Pete
Reply all
Reply to author
Forward
0 new messages