Module: Glimmer::LibUI::DataBindable
- Included in:
- AttributedString, ControlProxy, Shape
- Defined in:
- lib/glimmer/libui/data_bindable.rb
Overview
Parent controls and shapes who have children and add child post_initialize_child
Instance Method Summary collapse
-
#data_bind(property, model_binding) ⇒ Object
Sets up read/write (bidirectional) data-binding.
-
#data_bind_read(property, model_binding) ⇒ Object
Sets up read data-binding (reading from model to update view).
-
#data_bind_write(property, model_binding) ⇒ Object
Sets up write data-binding (writing to model from view).
- #data_binding_model_attribute_observer_registrations ⇒ Object
Instance Method Details
#data_bind(property, model_binding) ⇒ Object
Sets up read/write (bidirectional) data-binding
classes are expected to implement ‘data_bind_write(property, model_binding)` to setup write data-binding by observing view property for changes and writing to model attribute via model binding accordingly
classes can override data_bind_read to disable read data-binding in rare scenarios that might need it
returns model attribute reading observer registration by default
34 35 36 37 38 |
# File 'lib/glimmer/libui/data_bindable.rb', line 34 def data_bind(property, model_binding) data_bind_read(property, model_binding).tap do data_bind_write(property, model_binding) unless model_binding.[:read_only] end end |
#data_bind_read(property, model_binding) ⇒ Object
Sets up read data-binding (reading from model to update view)
Default implementation observes model attribute for changes via model binding and updates view property accordingly
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/glimmer/libui/data_bindable.rb', line 44 def data_bind_read(property, model_binding) model_attribute_observer = Glimmer::DataBinding::Observer.proc do new_value = model_binding.evaluate_property # TODO performance might be worse for not using block value instead (double double call on evaluate_property) send("#{property}=", new_value) unless send(property) == new_value end observer_registration = model_attribute_observer.observe(model_binding, attribute_writer_type: [:attribute=, :set_attribute]) model_attribute_observer.call # initial update data_binding_model_attribute_observer_registrations << observer_registration observer_registration end |
#data_bind_write(property, model_binding) ⇒ Object
Sets up write data-binding (writing to model from view)
Has no implementation by default. Classes are expected to implement this method by observing view property for changes and writing them to model accordingly via model binding
60 61 62 |
# File 'lib/glimmer/libui/data_bindable.rb', line 60 def data_bind_write(property, model_binding) # No Op by default end |
#data_binding_model_attribute_observer_registrations ⇒ Object
64 65 66 |
# File 'lib/glimmer/libui/data_bindable.rb', line 64 def data_binding_model_attribute_observer_registrations @data_binding_model_attribute_observer_registrations ||= [] end |