Wiki Markup |
{scrollbar} |
Excerpt |
|
Adding a type coercion to enable the If component to test for anything |
Extending the If Component
...
Code Block |
|
|
public class SearchResult<T> {
public final Class<T> itemType;
public final List<T> items;
public final int size;
public final int pages;
public final int firstIndex;
public final int lastIndex;
public SearchResult(Class<T> type, List<T> items, int size, int pages, int firstIndex,
int lastIndex) {
this.itemType = type;
this.items = items;
this.size = size;
this.pages = pages;
this.firstIndex = firstIndex;
this.lastIndex = lastIndex;
}
public boolean isEmpty() {
return size == 0;
}
}
|
...
In your templates, you have to check to see if the SearchResult exists, then see if it is empty, before you can get to the part that displays the content:
Code Block |
|
<t:if test="searchResult">
<t:if test="! searchResult.empty">
. . .
</t:if>
</t:if>
|
...
Code Block |
title |
AppModule.java (partial) |
|
|
public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration) {
add(configuration, SearchResult.class, Boolean.class,
new Coercion<SearchResult, Boolean>() {
public Boolean coerce(SearchResult input) {
return !input.isEmpty();
}
});
}
private static <S, T> void add(Configuration<CoercionTuple> configuration,
Class<S> sourceType, Class<T> targetType, Coercion<S, T> coercion) {
CoercionTuple<S, T> tuple = new CoercionTuple<S, T>(sourceType,
targetType, coercion);
configuration.add(tuple);
}
|
...
With this in place, the previous template can be simplified:
Code Block |
|
<t:if test="searchResult">
. . .
</t:if>
|
...