A link is a connection from one Web resource to another. Although a simple concept, the link has been one of the primary forces driving the success of the Web.Link Element, HTML4 spec
A JavaScript library for consuming HAL hypermedia APIs in the browser
var Resource = require('hyperagent').Resource;
var api = new Resource('https://api.example.com/');
{ "_links": { "self": { "href": "/" }
},
"ohai": "IAMA response"
}
api.fetch().then(function (root) {
console.log(root.props.ohai); // "IAMA response" });
{ "_links": { ... "curies": [ { "name": "ht", "href": "http://haltalk.herokuapp.com/rels/{rel}", "templated": true } ], "ht:signup": { "href": "/signup" }, "ht:me": { "href": "/users/{name}", "templated": true
}
}
root.links['ht:users'] === root.links['http://haltalk.herokuapp.com/rels/users'];
{ "_links": { ... "curies": [ { "name": "ht", "href": "http://haltalk.herokuapp.com/rels/{rel}", "templated": true } ], "ht:signup": { "href": "/signup" }, "ht:me": { "href": "/users/{name}", "templated": true
} }
root.link('ht:me', { name: 'mike' }).fetch().then(function (user) {
assert(user.props.username === 'mike');
});
<li ng-repeat="user in root.links.users">
<button ng-click="showUser(user)">{{ user.props.name }}</button>
</li>
$scope.showUser = function (user) {
user.fetch().then(function (user) {
$scope.currentUser = user;
});
};
bower install hyperagent-forms
Hyperagent.configure('loadHooks', [HyperagentForms.LoadHook]);
"_forms": { "ht:signup": { "href": "/signup", "method": "POST", "schema": { "$schema": "http://json-schema.org/draft-04/schema#", "description": "Create an account", "title": "signup", "required": [ "username", "password" ], "type": "object",
"properties": { "username": { "minLength": 1, "type": "string", "description": "The username you want to log in with." }, "password": { "type": "string", "description": "The password you want to log in with." }
} } } }
var api = Hyperagent.Resource('https://api-rw.example.com/');
api.fetch().then(function () {
var signup = new api.forms['ht:signup']({ username: 'mkelly' });
signup.data.username = 'overwrite';
if (signup.validate()) {
signup.submit();
} else {
console.error(signup.errors);
}
});
POST /signup HTTP/1.1
Host: api-rw.example.com
...
Content-Type: application/json
Content-Size: xxx
{
"username": "mkelly",
"password": "ilikehal"
}
doc = HypermediaDocument(block, None)
url = url_for('.block_resource', page_id=page_id, block_id=block_id)
doc.set_property('id', block_id)
doc.add_link('self', url)
doc.add_link('seo:app', url_for('.app_resource', app_name=app_name))
doc.add_link('seo:page', url_for('.page_resource', page_id=page_id))
doc.set_form('seo:delete_block', url, method='DELETE')
doc.set_form('seo:update_block', url, method='PUT', schema=BLOCK_SCHEMA)
return doc
MongoDB may have its flaws,
being slow at trying out ideas sure
isn't one of them.
$scope.submit = function submit(blockData) {
// page is a HyperResource
var form = new page.forms['seo:add_block'](blockData);
return form.submit();
}.then(function (result) {
// promise chaining is awesome, too.
var resource = result.loadResource();
displayBlock(resource);
});