5

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){
    ...
});
Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
delimited
  • 263
  • 1
  • 6

3 Answers3

10

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

Y.one('input.units').on(['keyup', 'change'], function (e) {
    // ...
});
Ryan Grove
  • 2,919
  • 1
  • 15
  • 11
1

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);
Pat
  • 2,228
  • 3
  • 24
  • 33
  • (sitting next to @delimited who can't comment on his on question) he's just done exactly that... was just hoping for a more elegant solution – Gareth Davis Sep 08 '10 at 15:02
  • Ahh... gotcha. I thought he was just trying to figure out a way to get the function to fire for both events. – Pat Sep 08 '10 at 15:17
  • A more elegant solution would be to wrap the whole thing in a `(function(){ ... })()` and capture the private `var actionFunction` as a closure thereby making it essentially anonymous. That's what the OP's after right? Preventing global namespace pollution? Wait, come to think of it this is YUI3. The code is already wrapped in a function. – slebetman Sep 08 '10 at 15:55
1

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.

lawnsea
  • 6,463
  • 1
  • 24
  • 19
  • 1
    Actually, it is possible. All you need to do is pass an array of event types. This solution is a great demonstration of how it's possible to bend YUI's internals to your will, though. – Ryan Grove Oct 09 '10 at 00:09