A small REST framework/helper for jQuery.
$(function() {
restly.defaults.endpoint = 'http://myapidomain/api/v1';
// All requests are ending now at: http://myapidomain/api/v1/...
restly.defaults.resource = 'users';
// All requests are ending now at: http://myapidomain/api/v1/users...
});
$(function() {
restly.defaults.endpoint = 'http://myapidomain/api/v1';
restly.get({
resource: 'users',
success: 'doSomethingCoolCallback'
});
});
function doSomethingCoolCallback(users) {
$.each(users, function(index, user) {
//...
});
}
$(function() {
restly.defaults.endpoint = 'http://myapidomain/api/v1';
restly.get({
resource: 'users',
'id': 1,
success: 'doSomethingCoolCallback'
});
});
$(function() {
restly.defaults.endpoint = 'http://myapidomain/api/v1';
restly.delete({
resource: 'users',
'id': 1,
success: 'doSomethingCoolCallback'
});
});
<script>
$(function() {
restly.defaults.endpoint = 'http://myapidomain/api/v1';
restly.post({
resource: 'users',
fields: 'nameField,lastNameField' // Will be used as data
success: 'doSomethingCoolCallback'
});
});
</script>
<input name="nameField" value="myName" />
<input name="lastNameField" value="myName" />
<script>
$(function() {
restly.defaults.endpoint = 'http://myapidomain/api/v1';
restly.put({
resource: 'users',
id: 1,
fields: 'nameField' // Will be used as data
success: 'doSomethingCoolCallback'
});
});
</script>
<input name="nameField" value="myName" />
<script>
$(function() {
$('button').restly(endpoint: 'http://myapidomain/api/v1');
});
</script>
<button type="button" class="btn"
data-method="post"
data-resource="users"
data-fields="nameField,lastNameField"
data-success="aCallbackFunction">
<i class="icon-trash"></i>
</button>
<input name="nameField" value="myName" />
<input name="lastNameField" value="myName" />
<script>
$(function() {
$('button').restly(endpoint: 'http://myapidomain/api/v1');
});
</script>
<button type="button" class="btn"
data-resource="users"
data-success="aCallbackFunction"
data-error="aErrorCallbackFunction">
<i class="icon-trash"></i>
</button>
<script>
$(function() {
$('button').restly(endpoint: 'http://myapidomain/api/v1');
});
</script>
<button type="button" class="btn"
data-id="1"
data-confirm="Really?"
data-method="delete"
data-resource="users"
data-success="aCallbackFunction">
<i class="icon-trash"></i>
</button>
<script>
$(function() {
$('button').restly(endpoint: 'http://myapidomain/api/v1');
});
</script>
<button type="button" class="btn"
data-id="1"
data-method="delete"
data-resource="users"
data-redirect="http://google.de">
<i class="icon-trash"></i>
</button>