Opened 6 years ago

Last modified 2 years ago

#16661 assigned feature

Allow registering for "all" events that object/element emits

Reported by: unasabovic Owned by: dylan
Priority: undecided Milestone: 1.15
Component: Events Version: 1.8.3
Keywords: Cc:
Blocked By: Blocking:

Description

Backbone has a feature to register for all events on an object using the special event "all" like so:

model.on("all", myCallback);

myCallback will now be called whenever model emits an event. This is handy when trying to build complex collections of objects that need to propagate all events that its components emit. Patch is attached. Docs would need to be updated too.

Attachments (1)

allEvents.patch (1.2 KB) - added by unasabovic 6 years ago.
Patch for on.js to allow registering for all events

Download all attachments as: .zip

Change History (7)

Changed 6 years ago by unasabovic

Attachment: allEvents.patch added

Patch for on.js to allow registering for all events

comment:1 Changed 6 years ago by bill

Component: GeneralEvents
Owner: set to Kris Zyp

It would be analogous to watch(callback) which watches changes to any properties.

I don't see how the patch would work though since no one is creating or calling this "onall" method. Also, even if it worked for objects extending Evented, it wouldn't work for DOMNodes, so it might confuse people.

comment:2 Changed 6 years ago by unasabovic

Nothing special needs to be done to create the onall method. You would just register like this:

element.on("all", myCb);

This is how it's done in backbone. It is only a convention that event name "all" means call me back on all events that element emits. Then when dispatching any event all we need to do is check if element has a registered onall callback and call it too. So code is only needed on dispatch.

Yes, this is mainly for classes that inherit (mixin) Evented. Such as data models etc. But I didn't see how to add it to Evented since Evented is not the one processing callbacks on emit.

Can you suggest how this could be added to Evented only?

comment:3 Changed 6 years ago by unasabovic

One thing I forgot to mention: watch() only works if some property value changes, but with custom events, events do not necessarily cause any property changes. For example, I could do

model.emit("syncError", args)

And no model properties would be changed. So watch() wouldn't work. Or am I wrong? If you suggest a way I could add this to Evented only I'll be happy to rework it and submit a new patch.

comment:4 Changed 6 years ago by bill

I assume you could add code to the Evented.emit() method to do this, rather than adding it to on itself, but I think people would still get confused since on(myEventedObj, "all", ...) would work but on(myDomNode, "all", ...) would not. It doesn't mean the change can't be done, but it's just a downside.

I agree that on() and watch() are different.

comment:5 Changed 3 years ago by dylan

Milestone: tbd1.12
Owner: changed from Kris Zyp to dylan
Status: newassigned

Consider whether or not this should be accepted for 1.12.

comment:6 Changed 2 years ago by dylan

Milestone: 1.131.15

Ticket planning... move current 1.13 tickets out to 1.15 to make it easier to move tickets into the 1.13 milestone.

Note: See TracTickets for help on using tickets.