Support and Documentation


Any view model you create is a subclass of the abstract class ViewModel and optionally an implementation of an interface based on your view.

In the following declaration, ArticleViewModel extends the abstract class ViewModel for an Article.

import com.psddev.cms.view.ViewModel;

public class ArticleViewModel extends ViewModel<Article> { }

ViewModel provides several methods and callbacks you can use to fine-tune a view's generation. For details, see ViewModel.

View model Lifecycle

The order of events in the view model's lifecycle is as follows:

  1. shouldCreate

  2. onCreate

  3. Extract data from model, optionally using createView or createViews.

The following sections describe these methods.


This method is called prior to onCreate, and indicates if Brightspot should actually generate the view when a client requests a particular model instance. The inherited value from the abstract class is true, which means Brightspot always generates the view for the requested object. You can introduce logic to test if Brightspot should continue creating the view. For example, if a user is not authorized to view an article, you can prevent Brightspot from generating the view.

protected boolean shouldCreate() {
   return user.isAuthorized() ? true : false;

This method is called after shouldCreate and before generating any of the view's components. You can use this method for retrieving or building additional content not included in the received model. For example, when a user requests an article, the model received by the view model includes properties pertaining to the article itself, typically heading, author, and body. To display a list of articles recently viewed by the user, or any other information not related to the article itself, implement additional logic in the onCreate method. The following snippet is an example of finding articles recently viewed by a user at the time of rendering the requested article.

public class ArticleViewModel extends ViewModel<Article>  {

    private List<Article> histViewedArticles;

    protected void onCreate(ViewResponse response) { 1
        histViewedArticles = historyItem.findByUser(user,20);

    protected List<Article> getArticleHistory() { 2
        return histViewedArticles;



Overrides the abstract method onCreate to retrieve the last 20 articles viewed by the current user. For details about the methods available from the ViewResponse parameter, see ViewResponse.


Returns the list of recently viewed articles.


This method creates a view using the specified view-model class and model. You can use this method to modularize your code. For example, you can have a single view model associated with all your content types. Regardless of the item a client requests, the single view model is run, and inside that view model you can identify the correct view model for creating the view.

if (model instanceof Section) {
    return createView(SectionViewModel.class, model);
} else {
    return createView(ArticleViewModel.class, model);

This method creates an Iterable over views using the specified view-model class and model. You can use this method to create a series of related views, such as a series of comments to an article, which you incorporate into a parent view.

 public class CommentListViewModel extends ViewModel<CommentList> {

     public Iterable<CommentsView> getComments() {
         return createViews(CommentsViewModel.class, model.getComments());


In the previous snippet creates an Iterable of comment views based on data provided by the model CommentList.

createView(CommentListViewModel.class, CommentListViewModel.getComments());

The previous snippet creates a single view comprised of the comments provided by the view model CommentListViewModel.