On Aug 21, 12:15 am, "Ken Snyder" <[EMAIL PROTECTED]> wrote:
> ...> I forked prototype, and started working on NodeWrapper/NodeListWrapper
> > -http://github.com/kangax/prototype/tree/master/src/element.js
> > Accompanying tests are there as well (although the test suite is far
> > from being complete).
>
> > --
> > kangax
>
> Exciting to see in code! What about adding caching instances and maybe even
> using a double-duty constructor instead of Class.create?  See what I mean
> below.
> - Ken Snyder
> Prototype.Element = function(element) {
>   if (this instanceof Prototype.Element) {
>     // constructor
>     element.__prototypeWrapper = this;
>     this.raw = element;
>   } else {
>     // "getInstance" function
>     // allow element to be string, dom node, or Prototype.Element instance
>     element = (typeof element === 'string' ?
> document.getElementById(element) : element);
>     if (element instanceof Prototype.Element) {
>       return element;
>     }
>     if (element && element.__prototypeWrapper) {
>       return element.__prototypeWrapper;
>     } else {
>       return new Prototype.Element(element)
>     }
>   }
>
> };
>
> $W = Prototype.Element;
>
> var myDiv = document.getElementById('someDiv');
> var myWrappedDiv = $W('someDiv');
> myWrappedDiv === $W(myDiv); // true
> myWrappedDiv === $W(myWrappedDiv); // true
> // although $W() is called 3 times, only one instance is created

Ken,

I wanted to keep $W consistent with the rest of the library - be a
simple shortcut for creating a new instance of NodeWrapper. Just like `
$H` returns `new Hash` and $R returns `new ObjectRange`, $W should
probably return `new NodeWrapper`. $W is also responsible for
"transforming" string parameter into an actual element (via
`document.getElementById`):

...
this.$W = function(element) {
  if (typeof element == 'string') {
    element = document.getElementById(element);
  }
  return new Prototype.Node(element);
};
...

Using such "proxy" helper ($W) also ensures NodeWrapper is always
called as a constructor. There are less chances that a user will
mistakenly call `NodeWrapper` as a function and pollute the global
scope. It, therefore, allows us to get rid of "this instanceof
Prototype.Node" check:

new Prototype.Node('foo'); // returns a wrapper
Prototype.Node('foo'); // boom! returns undefined and creates a global
`source`/`raw` property

Good point about passing a wrapper into a wrapper : ) Would it make
sense to return a wrapper itself or a "clone" of it? Would there ever
be a need for a cloned wrapper?

As far as caching, I'm afraid that storing reference to a wrapper on a
node itself could lead to circular references (which as we know "leak"
in IE's JScript). Having a separate cache-table and map elements to
wrappers by an element's id (or a custom property of an element) seems
more appropriate.

I also think that #update, #insert, #replace, etc. should allow to
accept a wrapper (besides an element or a string):

$W('foo').insert($W('bar'));

This should be as easy as giving wrapper `toElement`/`toHTML` methods
(since `update`/`insert` delegate to those when available)

--
kangax
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to