Methods

Any ViewModel 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.

ViewModel Lifecycle

The order of events in the ViewModel’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.

shouldCreate

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;
}

onCreate

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 ViewModel 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, you 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package content.article;

/* Imports... */

public class ArticleViewModel extends ViewModel<Article>  {

   private List<Article> histViewedArticles;

   @Override
   public String getBody() {
      return model.getBody();
   }

   @Override
   public String getHeadline() {
      return model.getHeadline();
   }

   @Override
   protected void onCreate(ViewResponse response) {
      super.onCreate(response);
      histViewedArticles = historyItem.findByUser(user,20);
   }

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

}

In the previous snippet—

  • Lines 19–23 override 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 appearing in line 20, see ViewResponse.
  • Lines 25–27 return the list of recently viewed articles.

createView

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

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

createViews

This method creates an Iterable over Views using the specified ViewModel 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.

1
2
3
4
5
6
7
 public class CommentListViewModel extends ViewModel<CommentList> {

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

 }

In the previous snippet, line 4 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 ViewModel CommentListViewModel.