Comments (1)
hi,
i think somehow i found a solution for the form data binding.
not pretty, but it works.
first i created a model js component which stores the data of the model:
`
class DataModel(flx.JsComponent):
data = flx.DictProp({}, settable=True)
def init(self):
super().init()
@flx.action
def update_data(self, data):
self._mutate_data(data, 'replace', -1)
def dispose(self):
self.data.clear()
super().dispose()
`
next a single binding component to handle the changes in both ways:
`
class DataBinding(flx.JsComponent):
model = flx.ComponentProp()
field = flx.ComponentProp()
key = flx.StringProp('', settable=True)
on_done = flx.BoolProp(False, settable=True)
def init(self, model, key, field, on_done=False):
super().init()
self._mutate_model(model)
self._mutate_field(field)
self._mutate_key(key)
self._mutate_on_done(on_done)
@flx.reaction('model.data')
def on_model(self, *events):
self.update_field()
@flx.action
def update_field(self):
self.field.set_text(self.model.data[self.key])
@flx.reaction('field.user_done')
def on_field_done(self, *events):
if self.on_done:
self.update_model(events[-1]['new_value'])
@flx.reaction('field.user_text')
def on_field_text(self, *events):
if not self.on_done:
self.update_model(events[-1]['new_value'])
@flx.action
def update_model(self, value):
data = {
self.key: value,
}
self.model.update_data(data)
def dispose(self):
self.model = None
self.field = None
super().dispose()
`
and at last the form with the bindings:
`
class Form(flx.Widget):
model = flx.ComponentProp()
bindings = flx.ListProp([], settable=True)
def init(self):
super().init()
self._mutate_model(DataModel())
with flx.FormLayout():
self.form_no = flx.LineEdit(title='No.:')
self.form_name = flx.LineEdit(title='Name:')
self.form_desc = flx.MultiLineEdit(title='Description:')
with flx.HBox():
self.reset_btn = flx.Button(text='Reset')
self.save_btn = flx.Button(text='Save')
flx.Widget(flex=1) # Add a spacer
self.bind(self.model, 'no', self.form_no, True)
self.bind(self.model, 'name', self.form_name, True)
self.bind(self.model, 'desc', self.form_desc, True)
data = {
'no': '1',
'name': 'Name',
'desc': 'Desc',
}
self.model.update_data(data)
@flx.action
def bind(self, model, key, field, on_done=False):
bd = DataBinding(model, key, field, on_done)
self._mutate_bindings([bd], 'insert', len(self.bindings))
@flx.reaction('reset_btn.pointer_click')
def reset_form(self, *events):
data = {
'no': '1',
'name': 'Name',
'desc': 'Desc',
}
self.model.update_data(data)
@flx.reaction('save_btn.pointer_click')
def save_form(self, *events):
print(repr(self.model.data))
def dispose(self):
for entry in self.bindings:
entry.dispose()
self.bindings.clear()
super().dispose()
class UI(flx.Widget):
def init(self):
super().init()
with flx.HBox():
self.form = Form(flex=1)
class App(flx.PyComponent):
def init(self):
super().init()
self.ui = UI()
if name == 'main':
app = flx.App(App)
app.launch('browser')
flx.start()
`
what is not working having a manager to handle the bindings
if i use the below manager instead of having the bind method on the form,
i'm getting "RuntimeError: DataBinding15 needs a session!"
`
class DataManager(flx.JsComponent):
bindings = flx.ListProp([], settable=True)
def init(self, model=None):
super().init()
# self._mutate_model(model)
@flx.action
def bind(self, model, key, field, on_done=False):
bd = DataBinding(model, key, field, on_done)
self._mutate_bindings([bd], 'insert', len(self.bindings))
@flx.action
def clear(self):
self._mutate_bindings([])
def dispose(self):
for entry in self.bindings:
entry.dispose()
self.bindings.clear()
super().dispose()
`
greetings
nolan
from flexx.
Related Issues (20)
- how to set a timer HOT 10
- the pycomponents has conflicts with js? HOT 8
- Run as a desktop app, how to close the incognito window? HOT 1
- How to Change the "Starting Flexx App"? HOT 2
- Run as a desktop app, how to hide or remove the close button on the browser ? HOT 1
- Flexx lacks of a lot of features HOT 4
- I couldn't find any example of sending ajax request and getting response HOT 1
- PlotlyWidget in offline mode HOT 2
- "Lost connection to server" on every little connection error/timeout HOT 6
- Is there a way to send data from my client side js widget to server side (for example images, videos)? HOT 5
- 使用freeze失败
- Error 404 on leaflet.py example HOT 9
- _mutate_xxx‘s bug or for some other reason HOT 2
- No certificate found HOT 2
- Lost connection with server: message too big (1009) HOT 1
- ui css:max-height < min-height HOT 6
- 'Too many open files' error HOT 14
- Use flx.create_element to create a label in the for loop, bind the onclick event, and dynamically transfer parameters. lambda fun error HOT 1
- use multiprocessing in flexx after freeze, The window keeps popping up HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flexx.