1
\$\begingroup\$

Please provide feedback on the correctness of this code. It should handle older versions of IE but how far back it goes I have not determined yet.

/**************************************************************************************************
EVENTS
*/

    // ... snip

    Priv.functionNull = function () {
        return undefined;
    };

    // createEvent
    Priv.createEvent = function () {
        if (doc.createEvent) {
            return function (type) {
                var event = doc.createEvent("HTMLEvents");
                event.initEvent(type, true, false);
                $A.someKey(this, function (val) {
                    val.dispatchEvent(event);
                });
            };
        }
        if (doc.createEventObject) {
            return function (type) {
                var event = doc.createEventObject();
                event.eventType = type;
                $A.someKey(this, function (val) {
                    val.fireEvent('on' + type, event);
                });
            };
        }
        return Priv.functionNull;
    };

    Priv.proto.createEvent = function (type) {
        return Priv.createEvent.call(this, type);
    };

    Pub.createEvent = (function () {
        return function (element, type) {
            var temp = [];
            temp[0] = element;
            Priv.createEvent.call(temp, type);
        };
    }());

    // addEvent
    Priv.addEvent = (function () {
        if (win.addEventListener) {
            return function (type, callback) {
                $A.someKey(this, function (val) {
                    val.addEventListener(type, callback);
                });
            };
        }
        if (win.attachEvent) {
            return function (type, callback) {
                $A.someKey(this, function (val) {
                    val.attachEvent('on' + type, callback);
                });
            };
        }
        return Priv.functionNull;
    }());

    Priv.proto.addEvent = function (type, callback) {
        return Priv.addEvent.call(this, type, callback);
    };

    Pub.addEvent = (function () {
        return function (element, type, callback) {
            var temp = [];
            temp[0] = element;
            Priv.addEvent.call(temp, type, callback);
        };
    }());

    //remove event
    Priv.proto.removeEvent = (function () {
        if (win.removeEventListener) {
            return function (type, callback) {
                $A.someKey(this, function (val) {
                    val.removeEventListener(type, callback);
                });
            };
        }
        if (win.detachEvent) {
            return function (type, callback) {
                $A.someKey(this, function (val) {
                    val.detachEvent('on' + type, callback);
                });
            };
        }
        return Priv.functionNull;
    }());

    Priv.proto.removeEvent = function (type, callback) {
        return Priv.removeEvent.call(this, type, callback);
    };

    Pub.removeEvent = (function () {
        return function (element, type, callback) {
            var temp = [];
            temp[0] = element;
            Priv.removeEvent.call(temp, type, callback);
        };
    }());
\$\endgroup\$
1
\$\begingroup\$

The good

Your code will handle any type of browser, is pretty easy to follow.

The bad

Pub.removeEvent will not work. That's because you define Priv.proto.removeEvent twice, whereas you probably want to define Priv.proto.removeEvent and Priv.removeEvent.

The ugly

You are using doc.createEvent which is deprecated, you should look into event constructors.

Furthermore

Because IE decided to do things differently for many event related things, a lot of people check as you do for the existence of a function and go from there, however your approach could be a little DRYer.

Priv.addEvent = (function () {
    if (win.addEventListener) {
        return function (type, callback) {
            $A.someKey(this, function (val) {
                val.addEventListener(type, callback);
            });
        };
    }
    if (win.attachEvent) {
        return function (type, callback) {
            $A.someKey(this, function (val) {
                val.attachEvent('on' + type, callback);
            });
        };
    }
    return Priv.functionNull;
}());

can be done as

Priv.addEvent = (function () {
  var addFunction = win.addEventListener ? 'addEventListener' : 'attachEvent',
      prefix      = win.addEventListener ? '' : 'on';

  return function (type, callback) {
    $A.someKey(this, function (val) {
      val[addFunction]( prefix + type, callback);
    });
  };
  return Priv.functionNull;
}());

This approach can be applied to addEvent, removeEvent and perhaps to 'createEvent'.

Finally, as always, you should steer away from someKey, just stick to the standard Array.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ "The good, The Bad, The Ugly and IE." Definately the right order. \$\endgroup\$ – James Khoury Jan 29 '14 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy