+
+
API works best by defining named API actions which can be converted to URLs for each request/
+
To do this you must define your endpoints once in your application before making requests. Usually this is done in a central configuration file included on each page.
+
URLs listed in your API can include required parameters and optional parameters which may be adjusted for each call.
+
Required Parameters will abort the request if they cannot be replaced when triggered.
+
Optional Parameters will be removed from the url if are not included when triggered. In addition, any trailing slashes before an optional parameter will also be removed from the URL, allowing you to include them in resource paths.
+
+ $.fn.api.settings.api = {
+ 'endpoint': 'api/{required}/{/optional}/{/optional2}'
+ };
+
+
+
+ /* Define API endpoints once globally */
+ $.fn.api.settings.api = {
+ 'get user' : '/user/{id}',
+ 'get followers' : '/followers/{id}?results={count}',
+ 'follow user' : '/follow/{id}',
+ 'add user' : '/add/{id}',
+ };
+
+
-
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 on click
or an input oninputchange
+
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
.
+
+
+ AJAX requests for the following demos have been faked using
SinonJS to avoid rate throttling from public APIs.
+
-
- $('.adopt.button')
- .api({
- action: 'follow user'
- })
- ;
+
+ $('.follow.button')
+ .api({
+ action: 'follow user'
+ })
+ ;
+
-
-
API couples well with ui state, a component used for managing text state and class names of elements.
-
State will automatically apply the class active
on API success, and update any text nodes with new values specified. This example updates the text of a follow button to "following" when the API action is successful, after flashing the text success.
-
- $('.adopt.button')
- .state({
- text: {
- inactive : 'Adopt',
- active : 'Adopted',
- deactivate : 'Undo',
- flash : 'Success'
- }
- })
- ;
+
+
+
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.
+
+ $('.follow.button')
+ .api({
+ action: 'get user',
+ on: 'hover'
+ })
+ ;
+
+
+
+
+
+
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.
+
+ $('.follow.button')
+ .api({
+ action: 'follow user',
+ on: 'now'
+ })
+ ;
+
-
+
-
When an API action occurs, templated url data is replaced with data you specify to be sent to the server.
-
-
+
+
+
Templated variables set in your API are replaced during your request in three different ways.
+
+
+
If many elements trigger a similar function, it is often easiest to include data attribute for each instance with the specific url variables
+
+
Follow User1
+
Follow User2
+
+
+
URL variables can be specified at run-time in the javascript object
+
+ $('.follow.button')
+ .api({
+ action: 'follow user',
+ urlData: {
+ id: 22
+ }
+ })
+ ;
+
+
+
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
+
+ $('.follow.button')
+ .api({
+ action: 'follow user',
+ beforeSend: function(settings) {
+ settings.urlData = {
+ id: 22
+ };
+ return settings;
+ }
+ })
+ ;
+
-
Or
-
Alternatively you can modify the data before sending to server
-
- $('.adopt.button')
- .api('setting', 'beforeSend', function(settings) {
- settings.urlData.id = 5209;
- return settings;
- })
- ;
+
+
+
+
+
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.
+
+
If state is initialized, it will automatically apply the class active
on API success, and update any text nodes with new values specified.
+
+
State also includes other states:
+
+ - Inactive - Default state
+ - Active - API request is completed succesfully
+ - Enable - 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
+
+
+
+ $('.follow.button')
+ .state({
+ text: {
+ inactive : 'Adopt',
+ active : 'Adopted',
+ deactivate : 'Undo',
+ flash : 'Success'
+ }
+ })
+ ;
+
+
+
+
+
+
+
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;
+ })
+ ;
+
+
+
+
+
+
+
+
-
-
-
A api can overlay page content instead of pushing it to the side
-
- $('.overlay.api')
- .api({
- overlay: true
- })
- .api('toggle')
- ;
-
-
-
-
-
- Setting |
- Default |
- Description |
-
-
-
- overlay |
- false |
- Whether api should overlay page instead of pushing page to the side |
-
-
- exclusive |
- true |
- Whether multiple apis can be open at once |
-
-
- useCSS |
- true |
- Whether to use css animations or fallback javascript animations |
-
-
- duration |
- 300 |
- Duration of side bar transition animation |
-
-
-
-
-
-
-
Callbacks specify a function to occur after a specific behavior.
-
-
-
- Setting |
- Context |
- Description |
-
-
-
- onShow |
- API |
- Is called when a api is shown. |
-
-
- onHide |
- API |
- Is called when a api is hidden. |
-
-
- onChange |
- API |
- Is called after a api changes visibility |
-
-
-
-
-
-
-
-
-
- Setting |
- Default |
- Description |
-
-
-
- namespace |
- api |
- Event namespace. Makes sure module teardown does not effect other events attached to an element. |
-
-
- className |
-
-
- className: {
- active : 'active',
- pushed : 'pushed',
- top : 'top',
- left : 'left',
- right : 'right',
- bottom : 'bottom'
- }
-
- |
- Class names used to attach style to state |
-
-
-
-
-
-
-
-
-
-
- Setting |
- Default |
- Description |
-
-
-
- name |
- API |
- Name used in debug logs |
-
-
- debug |
- True |
- Provides standard debug output to console |
-
-
- performance |
- True |
- Provides standard debug output to console |
-
-
- verbose |
- True |
- Provides ancillary debug output to console |
-
-
- errors |
-
-
- error : {
- method : 'The method you called is not defined.',
- notFound : 'There were no elements that matched the specified selector'
- }
-
- |
-
-
-
diff --git a/server/files/javascript/api.js b/server/files/javascript/api.js
index c9e291090..77241fb28 100755
--- a/server/files/javascript/api.js
+++ b/server/files/javascript/api.js
@@ -1,5 +1,14 @@
-semantic.api = {};
+/* Define API endpoints once globally */
+$.fn.api.settings.debug = true;
+$.fn.api.settings.api = {
+ 'get user' : '/user/{id}',
+ 'get followers' : '/followers/{id}?results={count}',
+ 'follow user' : '/follow/{id}',
+ 'add user' : '/add/{id}',
+};
+
+semantic.api = {};
// ready event
semantic.api.ready = function() {
@@ -17,7 +26,7 @@ semantic.api.ready = function() {
server.autoRespondAfter = 500;
server
- .respondWith(method, '/api/follow/5209', [responseCode, headers, body])
+ .respondWith(/\/follow\/(\d+)/, [responseCode, headers, body])
;
};