def save_grid(self, ev): def on_complete(req): if req.status == 200: # print(req.text) document["update-grid"].className = "ui right primary button" document.select("body")[0].toast({ "class": "success", "message": "You're using the good framework !" }) else: print("error ", req.text) ev.target.className = "ui loading button" displays = document.select(".displays") displays_data = {} for display in displays: displays_data[display.id] = display.innerHTML # print(displays_data) ajax.post( self.save_gird_url, data={ "displays": JSON.stringify(displays_data), "grid_id": self.grid_id, }, oncomplete=on_complete, headers={"Content-Type": "application/x-www-form-urlencoded"}, )
def send_ajax_request(obj, method, data, callback): global user_info def oncomplete(req): callback(JSON.parse(req.text) if req.status == 200 else {'error':req.text if req.text != '' else '未能从服务器获取信息'}) if 'user_info' not in data: data['user_info'] = user_info post_data = {'object':obj, 'method':method, 'data': JSON.stringify(data)} ajax.post('ajax', data=post_data, oncomplete=oncomplete)
def request(url, json, bind, method='POST'): """Send json data to backend.""" req = ajax.Ajax() req.bind('complete', bind) req.open(method, url, True) req.set_header('content-type', 'application/json') req.send(JSON.stringify(json)) return req
def change_state(task_id, new_state): json = {'state': new_state} req = ajax.Ajax() req.open('PATCH', f'/tasks/{task_id}', True) req.set_header('content-type', 'application/json') req.send(JSON.stringify(json)) return req
def refresh_UI(new_app_info): global app_info, current_activity, data_hub, user_info user, session_code, app, app_secret = user_info window.set_user_name(user) window.user_info = '' if app in ('me', 'ui', 'elt') and user != '': window.user_info = JSON.stringify({ 'user': user, 'session_code': session_code, 'app': app, 'app_secret': app_secret }) document["login"].hidden = (user == '') document["logout"].hidden = (user != '') for activity in app_info['activities']: document['nav-' + activity['id']].unbind('click') document["main-nav"].clear() app_info.update(new_app_info) for activity in app_info['activities']: a = html.A(activity['name'], **{ 'id': 'nav-' + activity['id'], 'class': 'nav-link' }) li = html.LI(a, **{'class': 'nav-item'}) document["main-nav"] <= li a.bind('click', lambda ev: show_activity(ev.target.id)) document["main"].clear() client = user_info[0] data_hub = DataHub(client if app_info['app'] == 'db' else '', True) for i, activity in enumerate(app_info['activities']): page = html.DIV(**{'id': 'page-' + activity['id']}) document["main"] <= page if activity['ui_json'] == '': pass else: make_ui(JSON.parse(activity['ui_json']), page, data_hub) page.style.display = 'none' if len(app_info['activities']) > 0: current_activity = None show_activity('nav-' + app_info['activities'][0]['id']) data_hub.onevent('', '', '') else: window.hide_spinner_modal()
def pop(self, key, default=_UnProvided()): if type(default) is _UnProvided or JSON.stringify(key) in self.storage: return JSON.parse(self.storage.pop(JSON.stringify(key))) return default
def get(self, key, default=None): if JSON.stringify(key) in self.storage: return self.storage[JSON.stringify(key)] return default
def __contains__(self, key): return JSON.stringify(key) in self.storage
def __setitem__(self, key, value): self.storage[JSON.stringify(key)] = JSON.stringify(value)
def __getitem__(self, key): return JSON.parse(self.storage[JSON.stringify(key)])
def __delitem__(self, key): del self.storage[JSON.stringify(key)]