Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in YUI3 is it possible to attach a single handler to multiple events?

so is something like this possible?

Y.one("input.units").on("keyup change", function(e){
    ...
});

the jquery equivalent is

$("input.units").bind("keyup change", function(e){
    ...
});
like image 492
delimited Avatar asked Sep 08 '10 14:09

delimited


3 Answers

Yes, this is possible. Just pass an array of event names instead of a string:

Y.one('input.units').on(['keyup', 'change'], function (e) {
    // ...
});
like image 113
Ryan Grove Avatar answered Sep 28 '22 03:09

Ryan Grove


Why not try something like this:

var actionFunction = function(e) { /* stuff goes here */ };

node.one("input.units").on("keyup", actionFunction);
node.one("input.units").on("change", actionFunction);
like image 24
Pat Avatar answered Sep 28 '22 04:09

Pat


EDIT: YUI supports this natively. See Ryan's answer below.

No. You could do something like this, though:

YUI().use("node", "oop", function (Y) {
var on = Y.Node.prototype.on;

function detachOne(handle) {
    handle.detach();
}

Y.mix(Y.Node.prototype, {
        on: function (type, fn, context) {
            var args = Y.Array(arguments),
                types = args[0].split(" "),
                handles = [];

            Y.each(types, function (type) {
                    args[0] = type;
                    handles.push(on.apply(this, args));
                })

            return {
                detach: Y.bind(Y.each, null, handles, detachOne)
            };
        }
    }, true);
})

This code wraps Node.on() to accept a string of space-delimited event types. It returns an object with a single method, detach, which detaches your handler from all of the events.

Note that this code only affects the Y instance inside its sandbox, so you should put it inside the function that you pass to YUI().use. It would also be easy to package it up as a module.

like image 31
lawnsea Avatar answered Sep 28 '22 04:09

lawnsea