BUG: handler is string but not function

master
Бородин Роман 2018-08-07 22:31:53 +03:00
parent ffc79d814c
commit 59adf589f5
3 changed files with 17 additions and 14 deletions

View File

@ -1,16 +1,15 @@
from orun.extjs import *
from orun.extjs import cp
def ok_click(id, *args, **kwargs):
cli << Ext.getCmp(id).setText('Clicked')
cli << js.client.alert('Server side message')
def ok_click(id_, *args, **kwargs):
js.cli << Ext.getCmp(id_).setText('Clicked')
js.cli << js.client.alert('Server side message')
def button_click(id, *args, **kwargs):
def button_click(id_, *args, **kwargs):
js.write("""
Ext.getCmp("%s").setText('Clicked');
alert('Server side callback message');
""" % id)
""" % id_)
class MyApplication(cp.ExtApplication):
def main(self, *args, **kwargs):
@ -18,7 +17,7 @@ class MyApplication(cp.ExtApplication):
'items': [{'xtype': 'button', 'text': 'Click Here', 'handler': button_click}],
'buttons': [
{'text': 'OK', 'handler': ok_click},
{'text': 'Close', 'handler': js.function('this.up("window").close();')}]})
{'text': 'Close', 'handler': js.function('this.up(\'window\').close()')}]})
wnd.show()
wnd.setHeight(200)

View File

@ -1,20 +1,22 @@
import json
from . import js
import re
__all__ = ['create', 'createByAlias', 'Component']
def js_ajax(fn):
i = id(fn)
js.live_methods[i] = fn
return js.client.Ext.Ajax.request({'url': js.AJAX_URL, 'method': 'GET', 'params': {'fn': i, 'id': js.client.this.id}, 'success': js.function('eval(arguments[0].responseText);')})
return "%s({'url': '%s', 'method': 'GET', 'params': {'fn': %d, 'id_': %s}, 'success': %s })"\
% (js.client.Ext.Ajax.request, js.AJAX_URL, i, js.client.this.id, js.function('eval(arguments[0].responseText);'))
js.js_ajax = js_ajax
def _create(meth, name, args):
#args['pyLive'] = True : TODO
obj = Component(**args)
js.write('var %s = Ext.create("%s", %s);' % (obj._id, name, str(obj)))
js.write('var %s = Ext.create(\'%s\', %s);' % (obj._id, name, str(obj)))
return obj
def create(name, args={}):
@ -24,10 +26,10 @@ def createByAlias(alias, args={}):
return _create('createByAlias', alias, args)
def get(id):
return js.JsNode('Ext.get("%s")' % id)
return js.JsNode('Ext.get(\'%s\')' % id)
def getCmp(id):
return js.JsNode('Ext.getCmp("%s")' % id)
return js.JsNode('Ext.getCmp(\'%s\')' % id)
class Component(js.JsObject):
def __init__(self, *args, **kwargs):
@ -53,4 +55,6 @@ class Component(js.JsObject):
pass
def __str__(self):
return json.dumps(self._js, default=js._encoder)
s = json.dumps(self._js, default=js._encoder)
return re.sub(r'("handler":\s+)("([^"]+)")', r'\1\3', s)

View File

@ -25,9 +25,9 @@ def encode(o):
return json.dumps(o, default=_encoder)
# trick json serialize javascript block
class JsBlock(int):
class JsBlock(str):
def __new__(cls, *args, **kwargs):
obj = super(JsBlock, cls).__new__(cls, 0)
obj = super(JsBlock, cls).__new__(cls, 'function () { %s }' % args[0])
obj.code = args[0]
return obj