Any element can have an API action attached, by default the action will occur on the most relevant event for the type of element. For example a button will use onclick
or an input oninput
.
Any element can have an API action attached directly to it. By default the action will occur on the most appropriate event for the type of element. For example a button will assume onclick
or an input oninput
, or a form onsubmit
.
$('.follow.button')
@@ -117,13 +129,13 @@ type : 'UI Behavior'
-
-
If you need to manually override what action an API event occurs on you can use the on
parameter. In addition, using the special parameter on: 'now'
will trigger the api event immediately.
+
+
If you need to override what action an API event occurs on you can use the on
parameter.
$('.follow.button')
.api({
- action: 'get user',
- on: 'hover'
+ action: 'follow user',
+ on: 'mouseenter'
})
;
@@ -131,7 +143,8 @@ type : 'UI Behavior'
-
If you need to manually override what action an API event occurs on you can use the on
parameter. In addition, using the special parameter on: 'now'
will trigger the api event immediately.
+
If you require API action to occur immediately use on: 'now'
. This will still trigger the same state updates to the invoked element, but will occur immediately.
+
$('.follow.button')
.api({
@@ -142,20 +155,81 @@ type : 'UI Behavior'
-
+
-
-
Templated variables set in your API are replaced during your request in three different ways.
+
+
If your API urls include templated variables they will be replaced during your request by one of four possible ways (listed in order of inheritance).
+
+ Only variables specified in your URL will be searched for in metadata. Adding metadata attributes will not be automatically included in GET or POST values.
+
+
-
-
If many elements trigger a similar function, it is often easiest to include data attribute for each instance with the specific url variables
+
+
+
Some useful values are automatically included in every request
+
+
+
+ Variable |
+ Description |
+ Available for |
+
+
+
+
+ text |
+ current text value of element |
+ All DOM elements |
+
+
+ value |
+ current input value of element |
+ All input elements |
+
+
+
+
+
+ // replaces /search?query={value}
+ $('.search input')
+ .api({
+ action: 'search',
+ // this setting will be explained later
+ stateContext: '.ui.input'
+ })
+ ;
+
+
+
+
+
+
If many elements trigger a similar function, it is often easiest to include unique url data in each triggering element. For example, many follow buttons will trigger the same endpoint, but each will have its own user id.
-
Follow User1
-
Follow User2
+
+ User 1
+
+
+ User 2
+
-
-
URL variables can be specified at run-time in the javascript object
+
+ $('.follow.button')
+ .api({
+ action: 'follow user'
+ })
+ ;
+
+
+
+
+
+
URL variables can be specified at run-time in the javascript object
$('.follow.button')
.api({
@@ -166,7 +240,10 @@ type : 'UI Behavior'
})
;
-
+
+
+
+
In addition all parameters can be adjusted in a special callback beforeSend
which occurs, quite intuitively, before the API request is sent.
You can also use this callback to adjust other settings before each API call
@@ -184,22 +261,101 @@ type : 'UI Behavior'
+
+
-
+
Many elements like button, input, and form have loading, disabled, and active states defined.
-
Learn more about states
-
-
API will automatically attach a Loading state when an API request is triggered, additional states can be attached using state behaviors, an optional component that couples well with API behaviors.
+
API will automatically attach a loading state when an API request is triggered, but makes no other assumptions about states.
+
-
If state is initialized, it will automatically apply the class active
on API success, and update any text nodes with new values specified.
+
+
+
If state()
is invoked after an API event is attached to an element, it will automatically toggle an active state on the element after a successful API request.
+
Basic included states
+
+
+
+
+ State |
+ Description |
+ API event |
+
+
+
+
+ loading |
+ Indicates a user needs to wait |
+ XHR has initialized |
+
+
+ error |
+ Indicates an error has occurred |
+ Request returns error (does not trigger onAbort caused by page change) |
+
+
+
+
-
State also includes other states:
+
+
+
+
Invoking state also includes additional states which can adjust text values:
+
+
+
+ State |
+ Description |
+ Occurs on |
+
+
+
+
+ inactive |
+ User has not selected |
+
+
+ active |
+ User has selected |
+ Toggled on succesful API request |
+
+
+ activate |
+ Text explaining activating action |
+ On hover if inactive |
+
+
+ deactivate |
+ default state |
+
+
+ hover |
+ Text-only state explaining interaction |
+ On hover if inactive or active |
+
+
+ disabled |
+ Indicates element is disabled |
+ Only triggered programatically |
+
+
+ Flash |
+ Text-only state used to display a temporary message |
+ Only triggered programatically |
+
+
+ Disabled |
+ Cannot receive user interaction |
+
+
+
- Inactive - Default state
- Active - API request is completed succesfully
- Enable - Text on hover if currently in inactive state
+ - Disable - Text on hover if currently in inactive state
- Deactivate - Text on hover if currently in active state
- Hover - Text appears on hover regardless of state
- Flash - Text appears on element for time duration set by
flashDuration
@@ -209,34 +365,15 @@ type : 'UI Behavior'
$('.follow.button')
.state({
text: {
- inactive : 'Adopt',
- active : 'Adopted',
- deactivate : 'Undo',
- flash : 'Success'
+ inactive : 'Follow',
+ active : 'Followed',
+ deactivate : 'Unfollow',
+ flash : 'Updated!'
}
})
;
-
-
-
When an API action occurs, templated url data is replaced with data you specify to be sent to the server.
-
-
Or
-
Alternatively you can modify the data before sending to server
-
- $('.adopt.button')
- .api('setting', 'beforeSend', function(settings) {
- settings.urlData.id = 5209;
- return settings;
- })
- ;
-
-