Wt: Wt::WViewWidget Class Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Member Functions | Protected Member Functions
Wt::WViewWidget Class Reference

An abstract base class for an MVC view that is rendered using a widget. More...

`#include <Wt/WViewWidget>`

Inheritance diagram for Wt::WViewWidget:
[legend]

List of all members.

## Public Member Functions
| WViewWidget (WContainerWidget *parent=0)
| Creates a new view widget.
void | update ()
| Updates the view.
virtual void | load ()
| Loads content just before the widget is used.
virtual void | render (WFlags< RenderFlag > flags)
| Renders the widget.
virtual void | refresh ()
| Refresh the widget.
## Protected Member Functions
virtual WWidget * | renderView ()=0
| Creates a widget that renders the View.

## Detailed Description

An abstract base class for an MVC view that is rendered using a widget.

In principle, Wt widgets are self-contained and manage both their content, behavior and layout. From the point of view of a Model-View-Controller (MVC) design pattern, they implement each of these, except for the view widgets that work in conjunction with WAbstractItemModel. As a developer you can chose to keep Model, View and Controller together or separate them as you wish.

This widget facilitates separation of the View from the Model and Controller in a particular way. The View is rendered as a Wt widget. The use of this widget provides two benefits. The classic MVC benefit is a decoupling between view and model, which may allow easier maintainance of code. In addition, this widget enforces the View to be stateless, as it is only created transiently on the server. Therefore the View does not require session resources. This may increase scalability for Internet-deployments.

The rendered View widget returned by renderView() should reflect the current model state. Whenever the model changes, rerendering can be triggered by calling update().

Currently, the View cannot enclose WFormWidgets which would allow direct manipulation of the model (but we are working to remove this limitation in the future, and let the Model/Controller handle editing changes) and the View may only be updated by a complete rerendering of the entire view.

The View widget may contain event handling code, but only in one of the following ways:

- event handling implemented directly in JavaScript code
- event handling implemented in pre-learned stateless slot implementations

Thus, currently, event handling code related to the View cannot be implemented at server-side (but we are thinking about a solution for this as well...).

Implementation example:

     // Shows the contents for a specific role of a model index in a WText widget
     class SourceView : public Wt::WViewWidget
     {
     public:
       // role is the ItemDataRole
       SourceView(int role)
         : role_(role)
       { }

       // set an index
       void setIndex(const Wt::WModelIndex& index) {
         if (index != index_
             && (!index.isValid() || !index.data(role_).empty())) {
           index_ = index;
           update(); // trigger rerendering of the view
         }
       }

     private:
       Wt::WModelIndex index_;
       int             role_;

     protected:
       virtual Wt::WWidget *renderView() {
         Wt::WText *result = new Wt::WText();
         result->setInline(false);

         if (!index_.isValid())
           return result;

         boost::any d = index_.data(role_);
         const std::string& t = boost::any_cast<const std::string&>(d);

         result->setTextFormat(Wt::PlainText);
         result->setText(t);

         return result;
       }
     };

### CSS

This widget does not provide styling, and can be styled using inline or external CSS as appropriate.

## Member Function Documentation

void Wt::WViewWidget::load | ( |  | ) | `[virtual]`

Loads content just before the widget is used.

When the widget is inserted in the widget hierarchy, this method is called. Widgets that get inserted in the widget hierarchy will be rendered. Visible widgets are rendered immediately, and invisible widgets in the back-ground (or not for a plain HTML session). This method is called when the widget is directly or indirectly inserted into the widget tree.

The default implementation simply propagates the load signal to its children. You may want to override this method to delay loading of resource-intensive contents.

During the life-time of a widget, this method may be called multiple times, so you should make sure that you do a deferred initializiation only once.

Reimplemented from Wt::WWebWidget.

void Wt::WViewWidget::refresh | ( |  | ) | `[virtual]`

Refresh the widget.

The refresh method is invoked when the locale is changed using WApplication::setLocale() or when the user hit the refresh button.

The widget must actualize its contents in response.

Reimplemented from Wt::WWebWidget.

void Wt::WViewWidget::render | ( | WFlags< RenderFlag > | flags | ) | `[virtual]`

Renders the widget.

This function renders the widget (or an update for the widget), after this has been scheduled using scheduleRender().

The default implementation will render the widget by serializing changes to JavaScript and HTML. You may want to reimplement this widget if you have been postponing some of the layout / rendering implementation until the latest moment possible. In that case you should make sure you call the base implementation however.

Reimplemented from Wt::WWebWidget.

virtual WWidget* Wt::WViewWidget::renderView | ( |  | ) | `[protected, pure virtual]`

Creates a widget that renders the View.

This method must be reimplemented to return a widget that renders the view. The returned widget will be deleted by WViewWidget.

Implemented in Wt::WStaticModelView< Renderer >.

void Wt::WViewWidget::update | ( |  | ) |

Updates the view.

Typically, the model will want to update the view when the model has changed.

This will trigger a call to renderView() to ask for a new rendering of the view.

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator