Giter Site home page Giter Site logo

eventdelegation's Introduction

Better Event Delegation

This extension is a fresh look at event delegation, taking into consideration limitations of the current MooTools event delegation approach.

Screenshot

How to use

window.addEvent('domready', function() {
            
    var first_event = function(e)
        {
            console.log('first clicked!');
        },
        second_event = function(e)
        {
            console.log('second clicked!');
            document.body.denyEvent('click', 'li.second', second_event);
        },
        third_event = function(e)
        {
            console.log('third clicked!');
            document.body.denyEvents('mouseover', 'li.third');
        };
    
    document.body.delegateEvents({
        'click': {
            'li.first': first_event,
            'li.second': second_event,
            'li.third': third_event
        },
        'mouseover': {
            'li.first': function(e) {
                console.log('first hovered!');
            },
            'li.second': function(e) {
                console.log('second hovered!');
            },
            'li.third': function(e) {
                console.log('third hovered!');
            }
        }
    });
    
    document.body.delegateEvent('click', {
        'li.first': function(e)
        {
            console.log('second delegate on first item!');
        }
    });
    
    document.body.delegateEvent('mouseover', {
        'li.third': function(e)
        {
            console.log('second delegate on third item!');
        }
    });
});

This code illustrates how to add event delegates to mulitple elements with a single event. The second call to document.body.delegateEvent doesn't attach a new click event but simply adds the event delegates to the current list. You can also use delegateEvents() to setup delegates for mulitple event types at the same time. The syntax is also a lot less ambiguous than the current :relay pseudo-selector.

Please note, however, that due to current limitations in Element.match (MooTools 1.2.4) element decendency is not taken into account so you will need to be specific with id's/classes because 'ul#main > li a' is not properly handled with Element.match. This should be fixed in MooTools 2.

eventdelegation's People

Contributors

arieh avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

james-emerton

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.