Opened 8 years ago
Last modified 4 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)
Change History (7)
Changed 8 years ago by
Attachment: | allEvents.patch added |
---|
comment:1 Changed 8 years ago by
Component: | General → Events |
---|---|
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 8 years ago by
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 8 years ago by
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 8 years ago by
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 5 years ago by
Milestone: | tbd → 1.12 |
---|---|
Owner: | changed from Kris Zyp to dylan |
Status: | new → assigned |
Consider whether or not this should be accepted for 1.12.
comment:6 Changed 4 years ago by
Milestone: | 1.13 → 1.15 |
---|
Ticket planning... move current 1.13 tickets out to 1.15 to make it easier to move tickets into the 1.13 milestone.
Patch for on.js to allow registering for all events