Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I created event delegation like concept which is present in jQuery. AFAIK, event delegation is used to register an event for an element which is supposed to be added dynamically. So, in jQuery we do the following:

$(document.body)
    .on('click', '.main', function(){ /* code here */ });
    .on('click', 'div.main', function(){ /* code here */ }); 
    .on('click', 'div#main.main', function(){ /* code here */ }); 
    .on('click', 'div#main.main[attr="1"]', function(){ /* code here */ });

So, instead of doing this event delegation in jQuery, I am doing using pure JavaScript. Here is my code:

var sb = (function () {
    var eventObject = {};
    document.body.addEventListener('click', function(e){
        console.dir(e.currentTarget);
        var el = e.target;
        var parent = el.parentElement;
        while(parent !== document.body){
            for(var item in eventObject){
                var elements = document.querySelectorAll(item);
                for(var i = 0, ilen = elements.length; i < ilen; i++){
                    if(el === elements[i]){
                        eventObject[item].call(el, e);
                        return;
                    }
                }
            }
            el = parent;
            parent = el.parentElement;
        }
    }, false);

    return {
        on: function(event, selector, callback){
            eventObject[selector] = callback;
            return sb;
        }
    }
})();

As you can see, in the above code, I have hard coded the click event (which is fine for now)

Now, I will use the above code like this:

sb
    .on('click', '.main', function(){ /* code here */ });
    .on('click', 'div.main', function(){ /* code here */ }); 
    .on('click', 'div#main.main', function(){ /* code here */ }); 
    .on('click', 'div#main.main[attr="1"]', function(){ /* code here */ });

This is working fine but I feel like I am overdoing things here in the above created plugin.

share|improve this question

The one thing different between your implementation and jquery.on is that jQuery allows you to bind on any element. One reason for that is performance. If on a deep tree, it would take a while for it to reach the delegate target. That's one reason jquery.live was deprecated.

Another is that your eventObject only accepts one callback per selector. If I were to register another callback under the same selector, then it would overwrite whatever was previously registered. Consider creating an array on the fly and push to it. You will need to change the way your code calls callbacks.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.