Hi istvan and the rest,

I can not get this to work. The point is that I want to call an object 
method when the change event in the DOM element is fired.
It must be an object method because it must process object data.

I am a bit confused. I am not sure that the jQuery event is at all aware of 
the object context, so I do not know how to call the change method of the 
object related to the Select2 component.

The idea is the following:

   1. The user enters data in the Select2 element, linked to a pyjs object
   2. Select2 fires a change event
   3. The change event is bound to a change method in the pyjs object
   4. That method gets called, in the object context
   5. The method processes the object data and performs the required 
   actions (in my case, send the updated data to the backend server via 
   JSONRPC)

I have tried several ways of binding the jQuery change event, so far 
without sucess (I call always afterwards JS(""" eval(@{{myjs}}) """))
I have also tried your suggestion wnd().change=change, but it did not help. 
Here are the errors that I get.

myjs = 'parent.jQuery("#%s").bind("change", this.change);' % (self.myid)
Uncaught TypeError: unbound method change() must be called with 
MySelect2TaggingComponent instance as first argument (got '[object Object]' 
instead)

myjs = 'parent.jQuery("#%s").bind("change", self["change"]);' % (self.myid)
Uncaught TypeError: unbound method change() must be called with 
MySelect2TaggingComponent instance as first argument (got '[object Object]' 
instead) 

myjs = 'parent.jQuery("#%s").bind("change", change);' % (self.myid)
jQuerySelect2 AttributeError: change is not defined

You can see the code (not yet working) in my fork: 
https://github.com/gonvaled/pyjs/blob/master/examples/jquery-select2/jQuerySelect2.py#L45

You can compile the jquery-select2 example like this: python jquery-select2 
--download (in the examples directory). Output goes to examples/__output__

Br,
Daniel

On Saturday, July 7, 2012 9:56:36 PM UTC+2, istvan.vago wrote:
>
> Daniel,
> try this:
>
> from __pyjamas__ import wnd
>
> class MySelect2TaggingComponent(HTML):
>
>     def change(self):
>         ...
>
>     wnd().change=change
>
> The change method will be accessible from jQuery as "change".
>
> Istvan
>
> 2012. július 6., péntek 11:51:59 UTC+2 időpontban Daniel Gonzalez a 
> következőt írta:
>>
>> Hi all,
>>
>> I am completing my integration of a jQuery component with pyjs, which I 
>> will share with you as soon as is ready. We could add that to the examples 
>> dir.
>>
>> Currently I am trying to pass a jQuery event up to my python code. The 
>> jQuery component is a Select2 element, which fires a change event whenever 
>> the user changes the content. I have verified that this is working by 
>> calling a pure javascript function, like this:
>>
>> class MySelect2TaggingComponent(HTML):
>>
>> ...
>>
>>     def setup_show(self):
>>         show = '''
>>          function show() {
>>                 var e=parent.jQuery("<div 
>> style='background-color:yellow;'>change 
>> fired</div>");
>>                 parent.jQuery("#%s").append(e);
>>                 e.animate({opacity:0}, 100000, 'linear', function() { 
>> e.remove(); });
>>             };''' % (self.myid)
>>         myjs = '%s parent.jQuery("#%s").bind("change", show);' % (show, 
>> self.myid)
>>         Logger("MySelect2TaggingComponent > setup_show", "Now calling 
>> JS: %s" % (myjs))
>>         JS(""" eval(@{{myjs}}) """)
>>
>> As you can see here, what I am doing is to define a show function and 
>> bind it to the element change event. This works.
>>
>> Now instead of calling the javascript show function, I want to call a 
>> pyjs "change" function, which will perform data processing and will send 
>> the data to the server.
>>
>> I would like to define a change method like this:
>>
>> class MySelect2TaggingComponent(HTML):
>>
>>
>>     def change(self):
>>         ...
>>
>>
>> How can I bind the javascript "change" event to this change method?
>>
>> Thanks,
>> Daniel
>>
>

Reply via email to