New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Events system #7
Comments
So 👍! The event system of v3+ is very obscure. |
If we are going to provide MV*, we should also implement |
This turned out to be vitally important for fixing Backbone memory leaks. CKEditor is a much more tightly controlled project, so it's probably okay to rely on institutional knowledge on how to prevent memory leaks, but still, it'd be better if it were done explicitly for you, and would probably help ensure that 3rd party CKEditor plugins also don't have memory leaks :) |
Following the above comments, which make sense, a general purpose events API is to be developed. The following is the interface of it:
And the new stuff (check Backbone):
Comments? |
Since we will have tree structure of models (thanks to collections) it would be powerfull to have events propagations just like in DOM. I also recommend you to fire event on object by this object itself. i.e. |
That's very interesting... but the problem is that, while the DOM deals with the same kind of objects in the tree (elements), we deal with totally different objects. For example: editor -> toolbar -> button. Let's suppose that "button" fires a "open" event, which is present also on "toolbar" and "editor" with totally different semantics. So if I listen to "open" in "editor", it would mean three different things, depending on what is the source of the event. In the DOM this is much clearer... if one element fires "click", this event still makes sense for its whole parent three because all parents can fire "click" with the very same semantics. So I'm a bit unsure about it still.
This is usually true... but it happens very often that one wants to simulate or force a behavior in an object, firing an event of it manually. So, IMHO, we should not enforce this to be private. |
I don't say that this pattern might work in all scenarios. Please note that in v5 we will have a lot of various models and collections, which we don't think about yet. This pattern might work even in toolbar > button scenario: https://gist.github.com/adelura/d72870b378fcf64e91fe Imagine that you have to add listener to each button when added and remove listener when button is removed.
Yep, but it might be just coding guideline |
@adelura, please note that I'm totally with you in this feature. I really like the idea... still I see problems. Ofc, the case you exemplified looks like the perfect world... but let's take the other case, when I don't want to listen to child events. Let's start with the DOM... When you listen for 'click' in an element (PARENT), it is ok to have it fired by a child (CHILD) element because while the 'click' happened in CHILD, it also happened (semantically) in PARENT. So if want to listen for 'click' in PARENT, I'm fine... and if I want to use PARENT to listen to 'click' in all CHILDs of a certain type, then I'm fine again. Now back to our case... let's suppose that we have the 'open' event in toolbar, which is fired when the toolbar opens after being collapsed (e.g.). Then let's suppose that I have 'open' in buttons as well, which is fired when a button opens a dialog (e.g.). If I do toolbar.on( 'open' ), what I'm listening to? If I want to catch the toolbar 'open', I don't have to care about checking in the source of the event is really the toolbar. Actually, I may not even be aware that buttons fire 'open' for different purposes as well. Confusion and WTFs in place because of this. I'm still a bit unsure about it because of all this. |
DOM is a great example how event propagation should work :) And I agree that not always custom event propagation is semantically correct.
Let me fix this up. See improved version https://gist.github.com/adelura/d72870b378fcf64e91fe I also introduced event static namespace which is pinned to constructor function. It brings readability. But what is more important is that default behaviour is the same as previous ones. However passing fourth argument into |
@delura, to summarize, you're proposing to introduce a new parameter to on(), so it'll look like this:
Then only if That's the one and only way to have it. Curious to hear others what they think about this feature in general. |
Btw, do we want to change the method signature in this case to something like this?
So |
I think we use
Also multiple times I needed to bind one callback to multiple events so it would be useful if I could do it:
|
I see that we use both... maybe priority a bit more often. Anyway, the proposed order doesn't come out of nothing. It is the common order found out there for similar APIs. The first 3 arguments are usually in that order. "priority" is something that we invented, that's why it went to after it. Finally, we've had this in this way since forever so I'm unsure it is worth changing such detail. |
I see this as a edge case, but it is doable. would just like to avoid bloating this API with features because this will be extensively used in our code. So here, less is better. Opinion from others is welcome. |
Talking about passing context to functions that receive callbacks... shouldn't we get rid of this, considering that one can now use |
And how exactly would you remove a listener then? You'd have to keep a reference to the bound function somewhere, while passing a regular function and the context would make the removal much easier. |
That makes sense. So we can take this inconsideration only for methods that accept callback but are note paired with a |
Not so edge IMO - I saw this repeating, especially in UI related code (recalculate something when whatever changed). And it's pretty simple to allow doing:
or more explicitly with an array of names. |
Actually, we have a feature in CKE4 that I like - the
I like this much more:
This is less important once you have |
I'm not a big fun of events propagation in the model or views. In my understanding there's a significant difference between DOM events and events in views. Let's consider a hypothetical structure:
Now, let's assume that click in the panel should fold it, but click in the help icon should show something else. If you listen on Also, note the semantical difference. Click on the panel means that you want to close it and click on the icon means that you want the help. DOM does not know about this, hence the propagation in its case makes sense. Last but not least, there must be an association between views to make propagation possible and only views know about it. So if we'll implement propagation, it will need to be integrated with the classes on which it happens. Hence, no need to think about this in relation to the general events API. |
However, I am a big fun of useful functions.
E.g. (
or, very useful events merging (all 3 events will cause firing
|
Actually, instead of:
we can do:
So if the listener is a string it's an event name to be fired. |
There may be couple of things that we would like to improve. Things I remember:
false
, then the event is cancelled. This is wrong for two reasons:false
but without an intention of cancelling the event; for example it may be some object's method.false
with cancelling it or may just miss it.The text was updated successfully, but these errors were encountered: