Skip to content
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

Add a function who checks if an event is binded #78

Open
gjuchault opened this issue Nov 15, 2012 · 6 comments
Open

Add a function who checks if an event is binded #78

gjuchault opened this issue Nov 15, 2012 · 6 comments

Comments

@gjuchault
Copy link

Hey, first of all nice job !

I would like to know if it was possible to add a function wich would be called "has" and would check if a elem has a event. Something like :

bean.on(elem, "click", function () {});
console.log(bean.has(elem, "click")); // true
bean.off(elem, "click");
console.log(bean.has(elem, "click")); // false

If you want to do this, just add line > 540 :

, has = function (element, event) {
    return registry.has(element, event);
}

and line 716 :

        , has               : has    

Note that it would be great if that function has could return true to something like that :

bean.on(elem, "click", function() {});
console.log(bean.has(elem)); // true
console.log(bean.has(elem, "c"); // false
console.log(bean.has(elem, "click"); // true

Thanks !

@rvagg
Copy link
Collaborator

rvagg commented Nov 16, 2012

Interesting idea, given that it'd be fairly low code overhead this would probably be ok I guess. Would you mind outline what kind of use-case you'd have for this though?
If you want to put together a pull request for this then I'll be happy to consider it but some basic tests would be appreciated if you can work out how the test suite works (I can help with that if you run into trouble--let me know).

@ded
Copy link
Collaborator

ded commented Nov 16, 2012

i'm interested in the use case too. from an application perspective, you should be able to use bean without thinking about that though. you should bind the event, and when the occasion arrises, bind the event again, and bean should be smart enough (already) to not add a duplicate event.

@rvagg
Copy link
Collaborator

rvagg commented Nov 16, 2012

welllll it just so happens that the dupe-detection feature was remove @ 1.0. See http://rod.vagg.org/2012/09/bean-v1/ - it was major suckage for performance and I made the decision that 1) it wasn't necessary for the vast majority code to justify the major overhead and 2) there may be legitimate reasons for adding the same handler more than once.

soooo, a has() method may make a bit of sense, but I'd still like to hear a use-case.

edited to remove poor humor

@gjuchault
Copy link
Author

Well, if you try to make a function cloneNode who copies all the children (with outerHTML or browser cloneNode), you'll see that thoses ways don't clone events. I tried bean.copy and it seemed to not to work or sth like that. So i added two function to your code : has and entries. entries returns the registry.

Here is my code :

for (allSourcesLength; allSourcesLength; allSourcesLength--) {
    registry = bean.entries();
    registryLen = registry.length;
    for (registryLen; registryLen; registryLen--) {
        if (registry[registryLen - 1].element === allSources[allSourcesLength - 1]) { // Seems to work without isEqualNode
            bean.on(allFinals[allSourcesLength - 1], registry[registryLen - 1].eventType, registry[registryLen - 1].original)
         }
    }
}

@rvagg
Copy link
Collaborator

rvagg commented Nov 16, 2012

perhaps you should just be doing something like this:

for (var i = 0; allSources.length; i++) {
  bean.clone(allSources[i], allFinals[i])
}

which should handle delegated events too and will just skip over elements that don't have events. I don't particularly want to leak the registry with an entries().

@gjuchault
Copy link
Author

I tried bean.clone, but it didn't work, and i don't have enough time to debug sorry :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants