> Wouldn't it be possible to have the better of the two worlds with
> something like this:

All sorts of options, yes. :-)  On reflection, I think the best course
would be to have a FakeElement class that has all of the properties
and methods of an extended element, but doesn't actually have anything
in the DOM.  You'd have to write it once (and I don't think you could
auto-write it by iterating the Element.prototype; too many
implementation hassles), and then add methods as the Prototype API
changed, which again isn't that often.  Writing it would probably take
an hour or two if you wanted to be smart about return values (for
instance, many of its methods need to return "this" rather than
undefined, to allow for chaining; getDimensions should probably return
{width: 0, height: 0} rather than undefined, etc.).

Then you just need a "nullsafe" version of $():

function $NS(element)
{
    element = $(element);
    return element ? element : new FakeElement();
}

Minimal runtime cost in the normal case of an element that *does*
exist (you can make it even more minimal by copying the core of $(),
it's just a few lines of code).  Dramatically less runtime cost that
Eric's and my earlier wrapping solutions.

Then you can happily code:

    $NS('elID').toggle();

...without caring whether 'elID' actually exists.  Again, this is only
for use in situations where that's really a valid thing.

What do you think, folks, can we drag this thread out further? :-)
--
T.J. Crowder
tj / crowder software / com

On Oct 23, 3:14 pm, Eric <[EMAIL PROTECTED]> wrote:
> Wouldn't it be possible to have the better of the two worlds with
> something like this:
>
> SafeElement=Class.create();
> for (e in Element.Methods)
> {
>
> SafeElement[e]=Element.Methods[e].wrap(function(originalFunction,element)
>   {
>         var args;
>
>         element = $(element);
>         if (!element)
>         {
>             return undefined;
>         }
>         args = $A(arguments);
>         args.shift();
>         args[0] = element; // We've already looked it up, save doing
> it again
>         return originalFunction.apply(this, args);
>   });
>
> }
>
> This way, we would have a new "SafeElement" class with all methods of
> Element except they have a safe behavior.
> So you can use Element.hide('xx') if you're sure 'xx' element exists
> or SafeElement.hide('xx') if you're not or if you're ok with the
> runtime overhead.
>
> This may perhaps be added to official prototype since it could be
> utile to many persons, and would have a very marginal impact when the
> library is loaded.
> What do you think?
>
> Eric
>
> NB: This is an untested example, but I am pretty confident it can be
> tweaked until it actually works :o)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to