views:

69

answers:

3

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){
    ...
});
+1  A: 

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
(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
Ahh... gotcha. I thought he was just trying to figure out a way to get the function to fire for both events.
Pat
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
+1  A: 

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
Great answer, thanks.
delimited
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
+2  A: 

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
Excellent, I have now changed this to be the accepted answer. Thanks to everyone who contributed.
delimited