You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
433 lines
16 KiB
433 lines
16 KiB
---
|
|
layout : 'default'
|
|
css : 'api'
|
|
|
|
title : 'API'
|
|
description : 'API allows elements to trigger actions on a server'
|
|
type : 'UI Behavior'
|
|
---
|
|
|
|
<script src="/javascript/library/sinon.js"></script>
|
|
<script src="/javascript/api.js"></script>
|
|
|
|
<%- @partial('header', { tabs: 'behavior' }) %>
|
|
|
|
<div class="main container">
|
|
<div class="ui active tab" data-tab="overview">
|
|
<div class="ui two column internally celled very relaxed stackable grid">
|
|
<div class="row">
|
|
<div class="column">
|
|
<div class="content">
|
|
<div class="ui header"><i class="green check icon"></i>Already Knows Your UI</div>
|
|
<p>Attach an API event to an input, by default, it will occur <code>oninput</code>. Attach an API event to a button, <code>onclick</code>. State management is built in: rate throttling, UI state changes like active, loading, disabled, min/max request lengths and more.</p>
|
|
</div>
|
|
</div>
|
|
<div class="column">
|
|
<div class="content">
|
|
<div class="ui header"><i class="green check icon"></i>Deal with resources not URLs</div>
|
|
<p>Create named actions like <code>'follow user'</code> and have API handle URL templating, parameters, and other annoyances for you.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="row">
|
|
<div class="column">
|
|
<div class="content">
|
|
<div class="ui header"><i class="green check icon"></i>State Management</div>
|
|
<p>Easily tie server events like AJAX loading elements using intuitive defaults based on the context inside your interface. Set maximum <b>and minimum</b> request times, toggle between UI states, and easily sync state between multiple elements with the same API actions.</p>
|
|
</div>
|
|
</div>
|
|
<div class="column">
|
|
<div class="content">
|
|
<div class="ui header"><i class="green check icon"></i>Server Traces For Humans</div>
|
|
<p>View your API request as it occurs in your web console, get errors if required url variables are missing, and useful performance metrics.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="ui intro tab" data-tab="usage">
|
|
<div class="fixed column">
|
|
<div class="demo content ui sticky">
|
|
<div class="ui fluid card">
|
|
<div class="image">
|
|
<img src="/images/avatar/large/stevie.jpg">
|
|
</div>
|
|
<div class="content">
|
|
<a class="header">Stevie Feliciano</a>
|
|
<div class="meta">
|
|
<span class="date">Joined in Sep 2014</span>
|
|
</div>
|
|
</div>
|
|
<div class="ui bottom attached follow button" data-id="22">Follow</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="examples">
|
|
|
|
<h2 class="ui dividing header">Defining Your API</h2>
|
|
|
|
<div class="no example">
|
|
<h4 class="ui header">Creating Server Actions</h4>
|
|
<p><b>API</b> works best by defining named API actions which can be converted to URLs for each request.</p>
|
|
<p>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.</p>
|
|
<p>URLs listed in your API can include <b>required parameters</b> and <b>optional parameters</b> which may be adjusted for each call.</p>
|
|
<div class="ui relaxed list">
|
|
<div class="item">
|
|
<div class="header">Required Parameters</div>
|
|
<div class="list">
|
|
<div class="item">Uses format <code>{variable}</code></div>
|
|
<div class="item">Will abort the request if they cannot be found.</div>
|
|
</div>
|
|
</div>
|
|
<div class="item">
|
|
<div class="header">Optional Parameters</div>
|
|
<div class="list">
|
|
<div class="item">Uses format <code>{/variable}</code></div>
|
|
<div class="item">Will abort the request if they cannot be found.</div>
|
|
<div class="item">Will be removed from the url automatically if not available.</div>
|
|
<div class="item">Any preceding slash before an optional parameter will be removed from the URL, allowing you to include them in resource paths.</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="code" data-type="javascript">
|
|
/* Define API endpoints once globally */
|
|
$.fn.api.settings.api = {
|
|
'get followers' : '/followers/{id}?results={count}',
|
|
'follow user' : '/follow/{id}',
|
|
'search' : '/search/?query={value}'
|
|
};
|
|
</div>
|
|
</div>
|
|
|
|
<h2 class="ui dividing header">Querying API Actions</h2>
|
|
|
|
<div class="ui info message">
|
|
<div class="ui header">Open Your Web Console</div>
|
|
The following examples work best while viewing logs in your web console. This experienced is optimized for Firebug, but will also appear in webkit browsers <a href="https://code.google.com/p/chromium/issues/detail?id=306120" target="_blank">with minor issues.</a>
|
|
</div>
|
|
<div class="ui message">
|
|
API requests for the following demos have been faked using <a href="http://sinonjs.org/">SinonJS</a> to avoid rate throttling from public APIs. No actual data is returned.
|
|
</div>
|
|
<div class="no example">
|
|
<h4 class="ui header">Attaching API to UI</h4>
|
|
|
|
<p>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 <code>onclick</code> or an input <code>oninput</code>, or a form <code>onsubmit</code>.</p>
|
|
|
|
<div class="evaluated code">
|
|
$('.follow.button')
|
|
.api({
|
|
action: 'follow user'
|
|
})
|
|
;
|
|
</div>
|
|
</div>
|
|
|
|
<div class="no example">
|
|
<h4 class="ui header">Specifying Events</h4>
|
|
<p>If you need to override what action an API event occurs on you can use the <code>on</code> parameter.</p>
|
|
<div class="code" data-demo="true">
|
|
$('.follow.button')
|
|
.api({
|
|
action: 'follow user',
|
|
on: 'mouseenter'
|
|
})
|
|
;
|
|
</div>
|
|
</div>
|
|
|
|
<div class="no example">
|
|
<h4 class="ui header">Calling Immediately</h4>
|
|
<p>If you require API action to occur immediately use <code>on: 'now'</code>. This will still trigger the same state updates to the invoked element, but will occur immediately.</p>
|
|
<p>
|
|
<div class="code" data-demo="true">
|
|
$('.follow.button')
|
|
.api({
|
|
action: 'follow user',
|
|
on: 'now'
|
|
})
|
|
;
|
|
</div>
|
|
</div>
|
|
|
|
<h2 class="ui dividing header">Providing Data</h2>
|
|
|
|
<div class="no example">
|
|
<h4 class="ui header">URL Variables</h4>
|
|
<p>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).</p>
|
|
<div class="ui ignored warning message">
|
|
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.
|
|
</div>
|
|
</div>
|
|
|
|
<div class="no example">
|
|
<h4 class="ui header">...automatically Routed Data</h4>
|
|
<p>Some useful values are automatically included in every request</p>
|
|
<table class="ui definition table">
|
|
<thead>
|
|
<tr>
|
|
<th>Variable</th>
|
|
<th>Description</th>
|
|
<th>Available for</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td>text</td>
|
|
<td>current text value of element</td>
|
|
<td>All DOM elements</td>
|
|
</tr>
|
|
<tr>
|
|
<td>value</td>
|
|
<td>current input value of element</td>
|
|
<td>All input elements</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<div class="code" data-preview="true">
|
|
<div class="ui search icon input">
|
|
<i class="search icon"></i>
|
|
<input type="text" class="search">
|
|
</div>
|
|
</div>
|
|
<div class="evaluated code" data-type="javascript">
|
|
// replaces /search?query={value}
|
|
$('.search input')
|
|
.api({
|
|
action: 'search',
|
|
// this setting will be explained later
|
|
stateContext: '.ui.input'
|
|
})
|
|
;
|
|
</div>
|
|
</div>
|
|
|
|
<div class="no example">
|
|
<h4 class="ui header">...by Data Attributes</h4>
|
|
<p>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.</p>
|
|
<div class="code" data-type="html">
|
|
<div class="ui follow button" data-id="11">
|
|
User 1
|
|
</div>
|
|
<div class="ui follow button" data-id="22">
|
|
User 2
|
|
</div>
|
|
</div>
|
|
<div class="code" data-type="javascript">
|
|
$('.follow.button')
|
|
.api({
|
|
action: 'follow user'
|
|
})
|
|
;
|
|
</div>
|
|
</div>
|
|
|
|
<div class="no example">
|
|
<h4 class="ui header">....in Javascript</h4>
|
|
<p>URL variables can be specified at run-time in the javascript object </p>
|
|
<div class="code" data-type="javascript">
|
|
$('.follow.button')
|
|
.api({
|
|
action: 'follow user',
|
|
urlData: {
|
|
id: 22
|
|
}
|
|
})
|
|
;
|
|
</div>
|
|
</div>
|
|
|
|
<div class="no example">
|
|
<h4 class="ui header">...returned values from beforeSend Callback</h4>
|
|
<p>In addition all parameters can be adjusted in a special callback <code>beforeSend</code> which occurs, quite intuitively, before the API request is sent.</p>
|
|
<p>You can also use this callback to adjust other settings before each API call</p>
|
|
<div class="code" data-type="javascript">
|
|
$('.follow.button')
|
|
.api({
|
|
action: 'follow user',
|
|
beforeSend: function(settings) {
|
|
settings.urlData = {
|
|
id: 22
|
|
};
|
|
return settings;
|
|
}
|
|
})
|
|
;
|
|
</div>
|
|
</div>
|
|
|
|
<h2 class="ui dividing header">Server Responses</h2>
|
|
|
|
<div class="no example">
|
|
<h4 class="ui header">Determining Success</h4>
|
|
<p>API is designed to work with APIs that return <code>JSON</code> objects. Instead of providiing success and failure callbacks based on the HTTP response of the request. A request is considered succesful only if the returned JSON value passes a <code>successTest</code>.</p>
|
|
<p>For example you might expect all successful JSON responses to return a top level property signifying the success of the response<p>
|
|
<div class="code" data-type="json" data-title="Server Response">
|
|
{
|
|
success: true
|
|
data: {
|
|
proprietaryData:
|
|
}
|
|
}
|
|
</div>
|
|
<p>Your success test recieves the servers json response, and should return true or false on whether the result should be considered successful. Succesful results will trigger <code>onSuccess</code> unsuccesful results <code>onFailure</code> but not <code>onError</code>, this is reserved for responses which return <a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest">XHR</a> errors.</p>
|
|
<div class="code" data-type="javascript">
|
|
$('.follow.button')
|
|
.api({
|
|
successTest: function(response) {
|
|
return response.success || false;
|
|
}
|
|
onSuccess: function() {
|
|
// valid response and action succeeded
|
|
},
|
|
onFailure: function() {
|
|
// valid response but action failed
|
|
},
|
|
onError: function() {
|
|
// invalid response
|
|
}
|
|
})
|
|
;
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<h2 class="ui dividing header">Controlling State</h2>
|
|
|
|
<div class="no example">
|
|
<h4 class="ui header">API State Management</h4>
|
|
|
|
<p>Many elements like <a href="/elements/button.html">button</a>, <a href="/elements/input.html">input</a>, and <a href="/collections/form.html">form</a> have loading, disabled, and active states defined.</p>
|
|
|
|
<p>States adjust class names on the triggering element or on the element specified by <code>settings.stateContext</code>.</p>
|
|
<p>Using <code>stateContext</code> allows you to easily do things like, trigger a loading state on a form when a submit button is pressed.</p>
|
|
|
|
<h5 class="ui header">States Included in API Module</h5>
|
|
<table class="ui definition table">
|
|
<thead>
|
|
<tr>
|
|
<th>State</th>
|
|
<th>Description</th>
|
|
<th>API event</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td>loading</td>
|
|
<td>Indicates a user needs to wait</td>
|
|
<td>XHR has initialized</td>
|
|
</tr>
|
|
<tr>
|
|
<td>error</td>
|
|
<td>Indicates an error has occurred</td>
|
|
<td>XHR Request returns error (does not trigger onAbort caused by page change, or if successTest fails). Stays visible for <code>settings.errorDuration</code></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<div class="no example">
|
|
<h4 class="ui header">Coupling with State Module</h4>
|
|
<p>Initializing an API action with the state module gives you more granular control over UI states, like setting an activated or de-activated state and the ability to adjust text values for each state:</p>
|
|
|
|
<p>
|
|
For additional examples of the possibilities available with state behaviors check the <a href="#">state module documentation</a>
|
|
</p>
|
|
|
|
<div class="code" data-demo="true">
|
|
$('.follow.button')
|
|
.state({
|
|
text: {
|
|
inactive : 'Follow',
|
|
active : 'Followed',
|
|
deactivate : 'Unfollow',
|
|
flash : 'Added follower!'
|
|
}
|
|
})
|
|
.state('setting', 'onActivate', function() {
|
|
$(this).state('flash text');
|
|
})
|
|
;
|
|
</div>
|
|
<h5 class="ui header">States Included in State Module</h5>
|
|
<table class="ui definition table">
|
|
<thead>
|
|
<tr>
|
|
<th>State</th>
|
|
<th>Description</th>
|
|
<th>Occurs on</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td>inactive</td>
|
|
<td>Default state</td>
|
|
</tr>
|
|
<tr>
|
|
<td>active</td>
|
|
<td>Selected state</td>
|
|
<td>Toggled on succesful API request</td>
|
|
</tr>
|
|
<tr>
|
|
<td>activate</td>
|
|
<td>Explains activating action</td>
|
|
<td>On hover if inactive</td>
|
|
</tr>
|
|
<tr>
|
|
<td>deactivate</td>
|
|
<td>Explains deactivating action</td>
|
|
<td>On hover if active</td>
|
|
</tr>
|
|
<tr>
|
|
<td>hover</td>
|
|
<td>Explains interaction</td>
|
|
<td>On hover in all states, overrides activate/deactivate</td>
|
|
</tr>
|
|
<tr>
|
|
<td>disabled</td>
|
|
<td>Indicates element cannot be interacted</td>
|
|
<td>Triggered programatically. Blocks API requests.</td>
|
|
</tr>
|
|
<tr>
|
|
<td>flash</td>
|
|
<td>Text-only state used to display a temporary message</td>
|
|
<td>Triggered programatically</td>
|
|
</tr>
|
|
<tr>
|
|
<td>success</td>
|
|
<td>Indicates user action was a success</td>
|
|
<td>Triggered programatically</td>
|
|
</tr>
|
|
<tr>
|
|
<td>warning</td>
|
|
<td>Indicates there was an issue with a user action</td>
|
|
<td>Triggered programatically</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="ui tab" data-tab="examples">
|
|
<p>Needs to be written still...</p>
|
|
<!-- Search Example !-->
|
|
|
|
|
|
<!-- Tab Manual Example !-->
|
|
|
|
|
|
<!-- Tab Auto Example !-->
|
|
|
|
|
|
</div>
|
|
|
|
<div class="ui tab" data-tab="settings">
|
|
<p>Needs to be written still...</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
</body>
|
|
</html>
|