react_to_props.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Example that demonstrates how you can write code that reacts to changes
in properties.
"""

from flexx import event


class MyModel(event.HasEvents):

    @event.prop
    def foo(self, v=0):
        return float(v)

    @event.connect('foo')
    def react_to_foo_a(self, *events):
        for ev in events:
            print('A: foo changed from %f to %f' % (ev.old_value,
                                                    ev.new_value))

    @event.connect('foo')
    def react_to_foo_b(self, *events):
        print('B: foo changed from %f to %f' % (events[0].old_value,
                                                events[-1].new_value))

m = MyModel()

m.foo = 3
m.foo = 7

event.loop.iter()