NAME

Rui::View::Base - view abstract base


SYNOPSIS

  # using views
  $view = $parentWidget->MyView; # use default model
  $view = $parentWidget->MyView(model => $model);
  $view->model($model);
  $model = $view->model;
  
  # subclassing: an on/off view
  package MyViews::OnOff;
  use Rui::Model::Value;
  use base 'Rui::View::Base';
  sub sendModelToClient
  {
     my $self = shift;
     $self->attribute(value => $self->model->value? 'On': 'Off');
  }
  sub getDefaultModel       { Rui::Model::Value->new }
  sub getDefaultWidgetClass { 'Label' }
  sub getModelEventNames    { qw(Change) }
  sub onUndefModel          { shift->clear }


SUPERCLASS

the Rui::Widget::Base manpage


REQUIRES

the Rui::Model::Base manpage


DESCRIPTION

A view is just a widget, but has some model. If the model is undefined, then the view is disabled. The view listens to model events, and modifies its display accordingly. It listens to events from the user, and modifies its model accordingly.

You can use model() to get/set the model of the view, and you can set it as an init key to the constructor. If created without a model, the view will create its own default model.

The view uses a the Rui::Model::Value manpage as a holder for the model. By setting the adapt key in the init hash, you can configure the view so that its model changes automatically whenever some event propagates through the value net. This saves you typing as there is no need to write the event handler that replaces the model, and the attach/detach mechanism for that event handler.


SUBCLASSING

You may implement the following template methods:

You must implement the following template methods:


COMPOSITING

Many times a composite view will want to give its children some part of its own model: in MVC there are parallel hierarchies of views and models. This means composite views need to replace the models of their child views when their own model is replaced. The framework does this for you if you do two things:

  1. Create the child views that you want to attach to parts of your model using the holder init() key, not the model key. You set the value to the name of the child model of the model of the composite view, that you want to give to the child view. Example: the Rui::View::DoubleList manpage creates a child list view on a part of its model so:
      $self->List(holder => 'left');

    Instead of:

      $self->List(model => $self->model->left);

    Now that the framework knows how to get the holder for the child model from the composite model, it can replace the models of the children views automatically when the composite view model is replaced.

    If the view you are creating is not a direct child of this view, you can use makeChildModelAdapter() to get an adapter on the child model.

  2. Add methods to the model of your composite view that return named child models that you want to give to the children views. Example: the Rui::Model::DoubleList manpage has a method getChildModel_left() that returns the selection in list that models the left list. The framework will call this method to get the new model for the child view when the model of the composite view is replaced.


METHODS

ACCESSING

model

parameters
Setting: model - the Rui::Model::Base manpage. A new model.

description
Get/set model. View is refreshed when model changes.

modelHolder

returns
the Rui::Model::Value manpage. The model holder.

description
The model holder is immutable for the view.

makeChildModelAdapter

parameters
Scalar. The name of the child model of the model of this view.

returns
the Rui::Model::Value manpage. An adapter on the child model.

description
Creates a new value model as an adapter on a child model of this view.