On Mon, Jun 18, 2012 at 5:45 AM, Maho <[email protected]> wrote:
> W dniu poniedziałek, 18 czerwca 2012 10:10:38 UTC+2 użytkownik C Anthony
> Risinger napisał:
>>
>> [... ]
>>
>> that sounds like a sound plan; you can embed JS('var x =
>> function(){}') blocks into your python, or IIRC, use something like:
>>
>> from __javascript__ import my_dispatcher
>
> Correct me if I'm wrong, but pyjs js code is imported into IFRAME context,
> so my_dispatcher has to be object/function in IFRAME, not in document,
> right?

yes, you are correct; each pygwt:module (we should really change this
to component/application or something to distinguish from python
modules) is loaded into an separate iframe.  i *think* you are correct
in that importing from __javascript__ will import from the application
frame [iframe] and not the main/top-level frame where the DOM is ... i
don't recall 100% off-hand but since you just did it i'll take your
word on it ;-)

>> ... to access to javascript identifiers.  also take a look at how the
>> stdlib and _pyjs.js use @{{python-identifier}} to embed python objects
>> into JS() blocks, so maybe something like (untested):
>>
>> def py_dispatcher(str_val, int_val):
>>     // do something ...
>>
>> JS('''
>>     $pyjs_dispatcher = function($string, $integer){
>>         @{{py_dispatcher}}(@{{str}}($string), @{{int}}($integer))
>>     }
>> ''')
>>
>> ... then you call $pyjs_dispatcher from your javascript code (with
>> primitive args), and it should properly call your python function.
>
> .... so, he shoulsn't call $pyjs_dispatcher, but
> document.frames[0].$py_dispatcher ...

yes, as-is, that's correct, but you can't really assume it'll be
frames[0] because there might be a history frame, another application
frame, or some other arbitrary iframe.  the "proper" way follows ...

> Am I wrong? I think not - I'm facing similar problem, and yesterday I tried
> to do something similar (well, opposite, access HTML objects from pyjamas
> code). And I had to do
>
>     from __javascript__ import window
>
> ....
>     window.parent.XXXX (where XXX is javascript identifier in main document
> code).

what you're looking for is:

from __pyjamas__ import doc, wnd

... doc and wnd (or $doc and $wnd in javascript) are references to the
main frame, NOT the application frame.  so, in our initial example,
you could simply change:

$pyjs_dispatcher = function($string, $integer){ ... }

... to:

$wnd.$pyjs_dispatcher = function($string, $integer){ ... }

... and then $pyjs_dispatcher properly exports to the main frame.
alternatively, you can use `--static-link=FILE` to link some arbitrary
javascript into the *application* frame [iframe], and then your JS
will execute in the same context as the pyjs/pygwt:module application.

nice catch :-)

-- 

C Anthony

Reply via email to