Actions
Appcelerator’s Web Expression Language (WEL) features many client-side actions that make it easier to build Rich Internet Applications (RIA). You can send messages, set the value of elements, bind a set of element values to the data returned by an Appcelerator service, affect layout, trigger effects like Fade and Appear, run javascript, and much more.
Message Action
An action can be an Appcelerator message. For Example:
on="click then r:message.request"
In the example above the message r:message.request will be sent when the element’s WEL expression condition, a click in this case, is triggered.
You can also set specific values in the data payload of the message.
on="[condition] then r:message.request[city=Atlanta]"
Then the message will be sent with city in its data payload. The value of city will be Atlanta.
Other examples of parameters include
on="[condition] then r:message.request[city=Atlanta,state=Georgia]"
on="[condition] then r:message.request[city=expr($('city_input').value)]"
Value Action
The value action sets the content of an element based on its type.
<div on="r:post.response then value[msg]"/>
Here, the content (or the innerHTML) of the div will be set to the value of msg from the payload of response.
The city action behaves differently depending on an element’s type.
| img | Sets the src attribute |
|
input (hidden) input (password) input (text) |
Sets the value of the input element |
| input ("checkbox") | Sets the checkbox value to true if the value of the payload parameter is "true" |
| select | Sets the list of drop down items. See the selected section. |
| textarea | Sets the content of the textarea |
| form | Sets the value of each child element based on its name attribute (which specifies the value to use from the message payload). |
| all other elements | Sets the content (or innerHTML) of the element |
Bind Action
The bind action sets the value of all elements with the same fieldset (read more about fieldsets).
You can do the following, which is a bit verbose:
<form>
<input fieldset="example" name="firstname" on="r:profile.response then value[person.firstname]"/>
<input fieldset="example" name="lastname" on="r:profile.response then value[person.lastname]"/>
<input fieldset="example" name="button" on="click then r:profile.save.request"/>
</form>
When r:profile.save.request is received, the input field values will be set from the data payload of the message.
At the end of the day, however, we like to save time by writing less code so we prefer the bind action:
<form fieldset="example" on="r:profile.response then bind[person]">
<input fieldset="example" name="firstname"/>
<input fieldset="example" name="lastname"/>
<input fieldset="example" name="button" on="click then r:profile.save.request"/>
</form>
This is useful when many elements have values that depend on the same message.
The bind action sets values just like the value action, so you can also use it for div elements, img elements, and so forth.
Set, Add, and Remove Actions
You can use the set action for setting CSS attributes, DOM attributes, or Javasript object attributes. For example
<img on="r:profile.response then set[src=image]"/>
The src attribute is set to the value of image in the payload of r:profile.response.
Other examples of the set action include:
<div on="r:items.response then set[border='1px solid #ccc']"/>
<select on="r:items.response then set[selectedIndex=0]"></select>
You can also use the add and remove actions to add or remove CSS classes or element attributes. For example:
<div on="r:items.response then add[class=selected]"/>
The class name selected is added to the div element.
<div on="r:items.response then remove[disabled]"/>
The disabled attribute is removed from the div element.
Layout Actions
| effect |
Execute a Scriptaculous effect.
Usage: on="[condition] then effect[effectName]"
|
| hidden |
Hide an element by changing its CSS visibility property to "hidden".
Usage: on="[condition] then hidden"
|
| hide |
Hide an element by changing its CSS display property to "".
Usage: on="[condition] then hide"
|
| show |
Show an element by changing its CSS display property to "block".
Usage: on="[condition] then show"
|
| visible |
Show an element by changing its CSS visibility property to "visible".
Usage: on="[condition] then visible"
|
| toggle |
Toggle an element's display or visibility property. You can also toggle
the state of any CSS property. In the latter case, the "off" state will
be a blank value for the property (e.g. border="")
Usage: on="[condition] then toggle[display=block]"
|
Other Actions
| blur |
Leave the current input element
Usage: on="[condition] then blur"
|
| click |
Simulate a button click
Usage: on="[condition] then click[id=my_button_id]"
|
| focus |
Set the focus on an input element
Usage: on="[condition] then focus"
|
| clear |
Clear the value of an input element.
Usage: on="[condition] then clear"
|
| clearform |
Clear the all the elements in a form from a form element
Usage: on="[condition] then clear"
|
| disable |
Disable an element
Usage: on="[condition] then disable"
|
| enable |
Enable an element
Usage: on="[condition] then enable"
|
| history |
Changes the hash of document location
Usage: on="[condition] then history[some_value]"
|
| reset |
Reset the value of an input element
Usage: on="[condition] then reset"
|
| select |
Select the contents of an input element
Usage: on="[condition] then select"
|
| submit |
Submit the form element
Usage: on="[condition] then submit"
|
| unselect |
Unselect the contents of an input element
Usage: on="[condition] then unselect"
|
Select Actions
You can populate a SELECT element (options for a drop-down menu) by using the value action.
<select on="l:items.response then value[property=rows,text=name,value=id]"></select>
where the data payload of l:items.response should look something like this
{rows: [{name: Atlanta, id: 1},{name: New York, id: 2},{name: Los Angeles, id: 3}]}
Another SELECT-specific action is selectOption which can be used to set the selected value based on the data payload.
<select on="l:select.request then selectOption[option]">
<option value="1">one</option>
<option value="2">two</option>
</select>
where the data payload would be
{option: 2}
Javascript Actions
| script |
Execute a Javascript statement
Usage: on="[condition] then script[alert('hello')]"
|