On Sep 3, 11:42 am, Jim Higson <j...@wikizzle.org> wrote:
> On Thursday 03 September 2009 16:29:33 Jim Higson wrote:
>
>
>
> > On Thursday 03 September 2009 06:51:19 kangax wrote:
> > > On Sep 2, 2:23 pm, Mojito <tokyot...@gmail.com> wrote:
> > > > What's the Prototyped syntax equivalent of:
>
> > > > window.frames['iFrameID'].document.getElementById
> > > > ('elementInsideIFrame');
>
> > > Current version of Prototype doesn't really support programmatic
> > > context extension (such as that of `window.frames['iframeID']` in your
> > > example). Instead, Prototype initialization for a given frame is
> > > usually accomplished by inserting script referencing prototype.js
> > > directly into that frame's document. Once loaded, parsed and executed,
> > > that script automatically extends frame's context with all of the
> > > prototype goodness. It's then possible to do something like -
> > > `window.frames['iframeID'].$$('...')`.
>
> > > Also, don't forget that most of the Prototype DOM abstractions do not
> > > play well with elements originating from different documents (since
> > > internally, Prototype practically always operates on "original"
> > > document - that which exists in a context where Prototype was
> > > initialized, not an actual document of an element).
>
> > Yes, It gets really confusing sometimes.
>
> > "So... which document is this Node from?"
>
> > "Is $ here the $ using the top frame's document or the iframe's document?"
>
> > "Ok, so this node is from the other document, which also loads Prototype,
> > but when I extended it with $ I added in functions which were scoped in
> > this document so it breaks, but since Prototype extends Element in Firefox,
> > if I hadn't have done that, it might have worked"
>
> > Some of this could be fixed if Prototype used element.ownerDocument instead
> > of just document (implicitly, window.document) for the Element methods.
>
> Actually, I'm starting to think this isn't really so. Some cases would work
> but there are a *lot* of references to document in the Prototype source and in
> many cases there isn't an element to query for its ownerDocument.

[...]

Well, using `ownerDocument` of an element passed to a method should
solve most of these problems. When method doesn't take an element, one
possibility is to pass document reference as an optional argument to a
method. For example, -

$('foo', otherDocument);

Unfortunately, things like these should really be accounted for from
the beginning (when designing API of a library). Prototype didn't
account for it in the beginning and made `$` a variadic function
(which actually has its own problems, but that's unrelated to
"context" issue :)). This design decision pretty much prevents
document passing as long as `$` has to be backwards compatible.

Another thing that can be employed is keeping publicly available
"current document" reference somewhere on Prototype (e.g.,
`Prototype.document`); then, internally, always access document via
that one single reference. This way, instead of:

foo(/*...*/, someDocument);
bar(/*...*/, someDocument);
baz(/*...*/, someDocument);

- user could do -

Prototype.document = someDocument;

foo(/*...*/);
bar(/*...*/);
baz(/*...*/);

or maybe even:

Prototype.withDocument(someDocument, function(){
  foo(/*...*/);
  bar(/*...*/);
  baz(/*...*/);
});

- where `withDocument` would set and unset certain document "around"
callback (second argument) execution.

Another problem with multiple contexts and Prototype is in its core
architecture. The fact that Prototype needs certain native objects to
be augmented makes it difficult to work with different contexts. For
example, Prototype augments `document` with `fire` method during its
initialization. It then, quite reasonable, assumes that
`document.fire` is always present. Now, if some method utilizes
`document.fire` (or, say, `Prototype.document.fire`), it's possible
that `Prototype.document` references "pure" unmodified document and
that this document has no `fire` at all. To work around that,
Prototype has to always use something like `Event.fire(document, ...)`
instead of `document.fire(...)` or, perhaps, augment document with
certain methods before trying to use them (if they don't exist).

--
kangax
--~--~---------~--~----~------------~-------~--~----~
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 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to