-
Notifications
You must be signed in to change notification settings - Fork 18
var
Defines a variable that can be used throughout the view in Java expressions and <script>
tags.
The <var>
tag can be placed anywhere in the view’s XML document, and you may include multiple <var>
tags with the same name, though these will refer to the same variable. It must include a type attribute to specify the type of the variable, and it may contain either a value attribute to explicitly set the value of the variable, or the lookup attribute to lookup the value from the controller heirarchy using a key.
Note
|
When using the lookup attribute, the type attribute is optional, as it can be inferred from the type of the lookup. |
<var name="VARIABLE_NAME"
type="VARIABLE_TYPE"
lookup="LOOKUP_KEY"
value="VALUE"/>
Variables are created as member variables of the view class. By default their visibility is set as package private meaning that the variable can be accessed by any class in the view’s package. You can assign a different visibility by wrapping the <var>
tag inside a <public>
, <private>
, or <protected>
tag.
E.g.
<public>
<!-- The following are public variables -->
<var name="user1" type="UserProfile"/>
<var name="user2" type="UserProfile"/>
</public>
<protected>
<var name="shoppingCart" type="com.example.myapp.models.ShoppingCart"/>
</protected>
<private>
<var name="myPrivateVar" type="int"/>
</private>
- name
-
The variable name.
- type
-
The type of the variable. E.g. a Java class name.
- lookup
-
An optional lookup key (a Java class) that can be used to lookup a value using Controller.lookup().
- value
-
An optional value to initialize the variable to. Values are interpreted as Java expressions unless prefixed with string:. It also accepts a java: prefix if you want to explictly declare that the value is a java expression, but this isn’t strictly necessary as java: is the default.
The following example uses the <var>
tag to define two variables: user1
and user2
. These variables are then used in the <script>
tag to assign values to them. They are then used again in the view-model
attribute of the <detailView>
to pass them as view models for these detail views.
<?xml version="1.0" ?>
<y xsi:noNamespaceSchemaLocation="EmbeddedDetailViews.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<import>
import com.codename1.rad.sampler.models.*;
import com.codename1.rad.sampler.providers.*;
</import>
<title>Embedded Detail Views</title>
<var name="user1" type="UserProfile"/>
<var name="user2" type="UserProfile"/>
<script>
user1 = new UserProfileImpl();
user2 = new UserProfileImpl();
user1.setName("Steve");
user2.setName("Shai");
</script>
<label>Detail View for user1</label>
<detailView view-model="user1"/>
<label>Detail View for user2</label>
<detailView view-model="user2"/>
<label>DetailView embedded in list view</label>
<entityList provider="SampleListProvider.class">
<row-template>
<!-- We explicitly reference the "rowModel" as the view-model here -->
<detailView view-model="rowModel"/>
</row-template>
</entityList>
<label>DetailView embedded in list view 2</label>
<entityList provider="SampleListProvider.class">
<row-template>
<!-- This demonstrates dependency injection as the rowModel will be used
as the view model implicitly here. -->
<detailView />
</row-template>
</entityList>
</y>