Rui::Model::Value - the value model
$value = Rui::Model::Value->new(value => 'foo'); $value->addListener(Change => $self); $value->value('bar'); # Change event will be fired $value->removeListener(Change => $self); $model = Rui::Model::Value->new( value => $value, events => { Change => $self, Value_Change => $self, }, ); $value->value('baz'); # Value_Change event will be fired $model->value(Rui::Model::Value->new); # Change event will be fired
$adaptee = Car->new(doorCount => 5); $adapter = Rui::Model::Value->new( adapt => [doorCount => $adaptee], value => 3, # Car::doorCount($adaptee, 3) called ); print $adapter->value; # 3, Car::doorCount($adaptee) called $adapter->value(4); # Car::doorCount($adaptee, 4) called
Value_
in front of the name of the event. See the description.
A value model allows listeners to be notified when a single value changes.
When this happens, it will fire the Change
event.
The value model will also propagate any events from its value to its
listeners. If you add listeners on event names that start with
Value_
, when the value fires, the value model will also fire these
events. Example: if you want the value model to propagate DoorOpened
events from its Car
value, listen to the event Value_DoorOpened
on
the value model.
Because you can listen to the value model and it will propagate events from its value, there is no need to add/remove listeners when the value is replaced. The value model does this for you. This simplifies the value net. If you were listening to the value of the value model, you would have to attach/detach when it is changed.
If you set the adapt => [$route => $adaptee]
key when
constructing, the value model will become an adapter on an aspect,
indicated by the router string, of the adaptee. Instead of holding its
value in itself, the model will get/set it from the adaptee using the
given route.
A value model will listen to the Erase
event of its value, if the
value is a model.
value - Optional. Any. My value.
adapt - Array ref of 2 elements. 1st is a route on the adaptee, 2nd is the adaptee. When getting/setting value, the value model will get/set its value from the adaptee, using the given route. The last segment of the route will be set with the new value when setting. Example:
Rui::Model::Value->new(adapt => ['value.{key}.property' => $foo]);
Will create an adapter that gets/sets its value from the object $foo
using the call chain: $foo->value->{key}->property
.
attachToAdaptee - Boolean, default false. If true the value model
will propagate all Change
events from its adaptee, as if they where
events of the value model. This is usefull as it makes the value model
look more like the adaptee for its clients.