En essayant de rendre mon Javascript discret, j'utilise onLoad
s pour ajouter des fonctionnalités à <input>
s et autres. Avec Dojo, cela ressemble à quelque chose comme:Existe-t-il un équivalent Javascript de Ruby's andand?
var coolInput = dojo.byId('cool_input');
if(coolInput) {
dojo.addOnLoad(function() {
coolInput.onkeyup = function() { ... };
});
}
Ou, environ équivalente:
dojo.addOnLoad(function() {
dojo.forEach(dojo.query('#cool_input'), function(elt) {
elt.onkeyup = function() { ... };
});
});
Quelqu'un at-il écrit une implémentation de Ruby andand pour que je puisse faire ce qui suit?
dojo.addOnLoad(function() {
// the input's onkeyup is set iff the input exists
dojo.byId('cool_input').andand().onkeyup = function() { ... };
});
ou
dojo.byId('cool_input').andand(function(elt) {
// this function gets called with elt = the input iff it exists
dojo.addOnLoad(function() {
elt.onkeyup = function() { ... };
});
});
Ou utilisez [CoffeeScript] (http://jashkenas.github.com/coffee-script/#operators): regardez "la variante accesseur de l'opérateur existentiel" '? .' :) –