Second action item from our hangout meeting was to find out if there
was interest in the community to spec out and possibly implement event
bus in angular.
To start off, can we gather some use-cases and scenarios in which
having an event bus would solve issues that can't be (easily) dealt
with in angular without an even bus?
/i
I like your prototype. The idea of figuring out which scopes were dead
is quite nice.
One big concern I have is that in your code the bus is synchronous.
That means that the execution of controller can be affected by some
bus listeners far in the distance. This might or might not be a
problem, but my intuition says that it would be better if the bus was
asynchronous. A naive way of achieving that would be by broadcasting
the event from within the $defer service. A better implementation
would require a hook in the scope life cycle (not a big deal).
Have you thought much about synchronous vs asynchronous behavior of the bus?
/i
> --
> You received this message because you are subscribed to the Google Groups "angular" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to angular+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/angular?hl=en.
>
>
Regards,
Witold Szczerba
On 29 July 2011 09:47, Igor Minar <iim...@gmail.com> wrote:
> Mainly, using scopes the way you did and scope's eval cycle to broadcast
> events is too heavy weight and creates issues that aren't obvious.
> For example calling $eval() on each $fireEvent is a sure way to run into
> problems, especially if $fireEvent is called from within an $eval() call
> (controller, defer, ng:click, etc).
Why is this a problem and how would you like to avoid it? What if
there is some implementation which does not explicitly call $eval on
$fireEvent? Would it mean that the result of listener execution does
not need $eval? I think whatever you would do inside listeners - they
would sooner or later trigger $eval anyway, so what is the point of
avoiding $eval from within $fireEvent?
> Additionally, it's not quite clear how the events should propagate through
> scope hierarchy. For example in this case:
> var root = angular.scope(),
> child1 = root.$new(),
> child2 = root.$new();
> child1.$on('foo', doSomething);
> child2.$fire('foo', 'bar');
> Should doSomething be called in this case or no? Logic would suggest that it
> shouldn't, but in order to make this bus practical, you likely do want it to
> propaga across scope boundaries. If you think otherwise, please share your
> argument with us.
This is almost exactly the case from my scenario test: $fire('foo',
...) should trigger child1's 'doSomething' - this is exactly what
event bus is for - to allow inter-scope communication. There is no
need for event bus to trigger something from within the same scope.
My exact use case was described in details on this form, that was when
I realized Angular does not support circular dependencies and event
bus was to come with a rescue:
http://groups.google.com/group/angular/browse_thread/thread/8bdef918969734c1
and this:
http://groups.google.com/group/angular/browse_thread/thread/20550bd1ffa24c87
> Implementing the bus as a service, makes it clear that all events are
> broadcasted globally.
> /i
This is how I wanted to implement event bus at the beginning, but
scopes live their own lives and singleton service stays forever, so I
figured out it would be easier not to bother with service doing
life-cycle tracking of scopes, but let the event bus be part of scopes
instead. Scope dies - so the event callback dies within.
Regards,
Witold Szczerba
On 29 July 2011 12:01, Vojta Jina <vojta...@gmail.com> wrote:
> I was thinking in the same way as you, but now I feel like separating this
> logic into service is better idea. What are the issues with having event bus
> as a service ?
> Some reasons:
> - easier handling global events (inter-scope communication even for)
> - clear api / separation - no messing up scope
> - performance ? - scope is crucial place, we can do some perf tests (there
> is test env for that in angular), but I believe your implementation is bit
> heavy...
ad.1) I am not sure if handling global events (do we have other kinds
of events than global?) is anyhow easier in service than in scope:
a) each scope has a reference to root
b) root can propagate to all the children
ad.2) That is questionable if API would actually be clearer, I think
the opposite:
a) first of all you would have to inject eventbus service
b) eventbus service would require 'scope' parameter (wouldn't it?)
So, instead of simple:
function MyCtrl() {
this.$onEvent('eventName', eventListener);
..
this.$fireEvent('otherEvent', parameter1, parameter2);
}
We would have to write:
function MyCtrl($eventbus) {
eventbus.on(this, 'eventName', 'eventListener');
..
eventbus.fire(this, 'otherEvent', parameter1, parameter2);
}
Do we actually require the 'this' parameter to be a scope object?
Could we get rid of the 'this' parameter? If yes - how would we know
when scope goes into oblivion? If 'this' could be something else - how
would the eventbus service figure it out? Like this: if source is an
instance of scope then track this scope and clean once it is gone?
I think scopes are so integral part of it: they emit events, they
handle event callbacks... Separating events would make sense if they
could be used outside of scopes but could they?
Thanks for exchange of opinions :)
Regards,
Witold Szczerba
> The main cons I saw, was removing dead listeners (when scope is removed),
> but now I kinda think it's not such a deal...
> Thanks for your ideas...
> V.
>
> --
> You received this message because you are subscribed to the Google Groups
> "angular" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/i4HTUMKe5U0J.
Sent from my iPhone
V.--
You received this message because you are subscribed to the Google Groups "angular" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/UqWLOFvtk3sJ.
- as a matter of interest are you guys still planning on implementing
a destructor approach to GC on scopes?
- how much can be done with the inbuilt injector in angular, is it
bound strictly to services or can its use be expanded upon?