You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@isis.apache.org by "Dan Haywood (JIRA)" <ji...@apache.org> on 2017/10/13 10:04:00 UTC

[jira] [Created] (ISIS-1747) Update docs, hints-n-tips for view models

Dan Haywood created ISIS-1747:
---------------------------------

             Summary: Update docs, hints-n-tips for view models
                 Key: ISIS-1747
                 URL: https://issues.apache.org/jira/browse/ISIS-1747
             Project: Isis
          Issue Type: Improvement
    Affects Versions: 1.15.1
            Reporter: Dan Haywood
            Priority: Minor
             Fix For: 1.16.0


see https://github.com/incodehq/incode-platform/issues/10

~~~~~~~~~

here's the text that has been removed

## Demo App: Highlighting Current

As a by-the-by, the demo app has one further "trick up its sleeve".
If you run the app you'll notice that the currently selected `DemoObject` is highlighted in the left-hand table of the `HomePageViewModel`.

This is accomplished by having the view model collaborate with a subscribing domain service that configures a CSS class.

We start by ensuring that the `DemoObject` emits an event for its CSS class:

.DemoObject.java
````java
@DomainObjectLayout(
        ...
        cssClassUiEvent = DemoObject.CssClassUiEvent.class
)
public class DemoObject ... {

    public static class CssClassUiEvent
            extends org.apache.isis.applib.services.eventbus.CssClassUiEvent<DemoObject> {}
    ...
}
````

Next, we define the domain service to act as the subscriber.
Since it will be interact

.HomePageViewModel.java
````java
public class HomePageViewModel ... {

    @DomainService(nature = NatureOfService.DOMAIN)
    public static class CssHighlighter extends AbstractSubscriber {

        @EventHandler
        @Subscribe
        public void on(DemoObject.CssClassUiEvent ev) {
            if(getContext() == null) {
                return;
            }
            if(ev.getSource() == getContext().getSelected()) {      // <1>
                ev.setCssClass("selected");
            }
        }

        private HomePageViewModel getContext() {                    // <2>
            return (HomePageViewModel) scratchpad.get("context");
        }
        void setContext(final HomePageViewModel homePageViewModel) {
            scratchpad.put("context", homePageViewModel);
        }

        @Inject
        Scratchpad scratchpad;                                      // <3>
    }
}
````
<1> If the domain object is the currently selected then set the CSS class
<2> Provide methods to set and get the current `HomePageViewModel` (acting as the context)
<3> Store the context using the `Scratchpad` domain service (request-scoped so thread-safe).

The `HomePageViewModel` is responsible for setting itself as the context for the domain service:

.HomePageViewModel.java
````java
public class HomePageViewModel ... {
    ...
    public TranslatableString title() {
        cssHighlighter.setContext(this);    // <1>
        ...
    }
    ...
    @javax.inject.Inject
    CssHighlighter cssHighlighter;
}
````
<1> set the context on the domain service


Finally we just need some CSS, in the `application.css` file:


.application.css
````css
.selected {
    font-style: italic;
    font-weight: bolder;
}
````



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)