-
Notifications
You must be signed in to change notification settings - Fork 211
jquery-select2 and pyjs #752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
…t use the same wnd().change
|
bleh im hesitant to include stuff like this because i don't want to encourage it ... and it's subject to change ... but we'll do it once ;-) i can't give this proper review right at the moment, but here are a couple first-pass changes:
|
|
Thanks Anthony. Still not clear to me is the JS def get_val(self):
# This uses select2 to get the value of the element
myjs = 'parent.jQuery("#%s").select2("val");' % (self.myid)
return JS(""" eval(@{{myjs}}) """)I am preparing a branch witha single commit for this, and as soon as this is clarified I will send a pull request. |
|
sorry i've let this stall for awhile -- i wanted to make sure i could review it proper. my son is at gradma's Wed - Sun, giving me a block of time to catch up on stuff. as such, i intend on giving this attention sometime in the next 2 days or so ... thanks much :-) |
|
so you've decided not to see this thru then i take it? i wasn't purposfully stalling ;-) it's just a sensitive subject and needed proper attention. anyways, i have a block of time tomorrow i intended to direct at this -- LMK ASAP if this was a mistake, else we'll just let it go, no worries either way. thanks gonvaled! |
|
Oooops! My bad. It seems I closed the pull request which is not at all what I wanted! I was writing a comment an it seems I pushed the wrong button. :( I repeat here my comment: "Good that you have some time for this. If you could comment on the get_val question about the JS directive, I can provide again a pull request with a squashed commit of all my changes - that JS bit is the only thing missing now" |
|
ok @gonvaled, thanks for being so patient. here are a couple simple things that should be fixed:
... i'll follow up with some examples on how to fix the |
|
Sure Anthony, all those things are fixed on my alternate branch, which I am not showing to you because I want to do it in a single commit (as you correctly pointed out last time). I still haven't committed because of the missing solution for eval. |
|
ok ... for the most part your example is pretty cool, nice job :-) now, even though most of this kinda makes me feel yucky, or requires some black magic to pull off, here goes ... i tested these some, and they seem to work as intended (if not, LMK and we'll work thru it). i'm only demonstrating the fairly correct(?) way: def update(self, values):
from __pyjamas__ import wnd
from __pyjamas__ import Array
from __pyjamas__ import Object
values_js = Array()
for value in values:
jsob = Object()
jsob.id = value['id']
jsob.text = value['text']
values_js.push(jsob)
wnd().jQuery('#' + self.myid).select2('val', values_js)... amazingly, that does the trick. blasphemies like this essentially work because we take many shortcuts in the name of performance and simplicity; for example, "python" strings are still JS strings, anything visible to JS can technically be imported from ... basically, you have insider knowledge and are taking sweet advantage of it ;-) somewhat(?) correct way: def update(self, values):
sid = self.myid
# identical ... look at output JS ;-)
sid = JS("@{{getattr}}(@{{self}}, 'myid')")
JS('values_js = Array()')
for value in values:
_id = value['id']
_text = value['text']
JS('jsob = {}')
JS('jsob.id = @{{_id}}')
JS('jsob.text = @{{_text}}')
JS('values_js.push(jsob)')
JS("$wnd.jQuery('#' + @{{sid}}).select2('val', values_js)")... this one is the polar opposite: using raw of course, anything using lastly, i'm pretty sure LMK if issues/questions. thanks @gonvaled! |
|
I see why it took you some time to prepare this! :) Not a simple one-liner ... I will try this later today, but I have some comments:
|
yeah i know the first seems preferable :-) one problem is, as you've stated, we are integrating a JS component so we cannot be free of JS (and to be fair, if one stays in Python-land, one can be nearly 100% free of JS :-). however, the core problem is the first method is a complete hack, and basically exists for demonstration only ... i guess i lied when i said i didn't know which was better, as nowhere else in the entire library exists such code. there are only a handful of objects that are valid imports from basically, if we are going to offer such solutions to people seeking examples, they should be the most robust/proven/supported. the first method needlessly runs the JS objects thru several Python-land functions (look at the generated code), each of which may introduce bugs and obscurities i'd rather not point out to people in the future. now, the translator could likely be improved to track these things better, but that support does not currently exist. alas, if you'd like to document both methods (caveats included) that would be perfectly fine, and useful for others' understanding, but it would have to emphasize which is the "correct-y-ish" way.
well, i suppose for all the reasons people tend to avoid def update(self, values):
sid = self.myid
JS('values_js = Array();')
for value in values:
_id = value['id']
_text = value['text']
JS("""values_js.push({id: @{{_id}}, text: @{{_text}}});""")
JS("$wnd.jQuery('#' + @{{sid}}).select2('val', values_js);")... which IMO anyway, is much clearer than building a string + eval'ing, even if you didn't know what was going on. ultimately though, if it works i suppose it works, but for an example i want to detail the most "proper-esque" way possible -- to help people succeed -- i don't wan't to disillusion them with methods that "appear to work" or "probably work", esp. if another method is superior/better supported.
aside from the crux of the issue is you're dropping a level and "gaming" the system, so-to-speak. it's really no different than using assembler within C code -- it only works in certain contexts, certain environments, certain machines/processors/etc: the analogy holds. one of the things i want to accomplish is a 100% asynchronous engine that allows for "pausing" the Python runtime, thus making such things as porting the Threading modules and true XHR dynamic imports possible and/or simpler ... to achieve this end, we'd potentially compile an entire python application/runtime down to a single (or minimal) switch + while loop (which is also, conveniently, a massive performance boost) ... in this context consider JS "compressor"/minification technology -- ie. renaming/rearranging all of your variables/attrs/etc -- how could you effectively use i guess the simple answer is: no one has come up with or stepped up with a clear way to achieve stability in this area. a good primer/case-study is the libffi project (foreign function interface) which is an interesting library and also happens to be the foundation of the python good stuff :-) |
|
i should clarify in case it's not clear: the dragons be there... what you see today may change tomorrow with no obligation for stability (but in practice you will be told, and for the most part it's fairly stable...ish). |
== Description ==
This is an example of how to integrate a complex jQuery component with pyjs.
Here we integrate Select2 to implement a tagging component.
Not all functionality of Select2 is implemented. Only:
== Issues ==
pyjd is not supported (doesn't do javascript)
The CSS is not right