Hi,
we want to express the relationship between links to our current schema. That's why we would like to introduce "target" as the official way to do that - analogous to the same attribute in <a>.
Let's say we have the following data:
{
"schema": {
"links": [
{
"rel": "self",
"href": "users/1",
"method": "GET",
"target": "_self",
"targetSchema": { /* the user itself */ }
},
{
"rel": "update",
"href": "users/1",
"method": "PUT",
"target": "_self",
"schema": { /* the writeable properties of user */ },
"targetSchema": { /* the user itself */ }
},
{
"rel": "car",
"href": "cars/17",
"method": "GET",
"target": "_blank",
"targetSchema": { /* some car */ }
},
{
"rel": "kill",
"href": "users/1/kill",
"method": "POST",
"target": "_parent",
"targetSchema": { /* some arbitrary data... maybe just a success message */ }
}
]
}
}
And let's say we use a nice JavaScript framework similar to Restangular:
$user.self(); // GET users/1
$user.update(data); // PUT users/1
$user.car(); // GET cars/17
$user.kill(); // POST users/1/kill
Note: With "rel": "kill" we don't want to remove (aka DELETE) the user, but maybe set some isAlive field from true to false. Let's say this isAlive field is read-only for "rel": "update", because it is a irreversible action and we have some special server-side logic to do this. So a normal PUT isn't an option.
Thanks to "target" we know that
- with "_self" we can update our data object in-place, because the response is the "same" resource as before
- with "_blank" we return a completely new data object, because the response is a completely different resource
- with "_parent" we manually reload the data object (with its self link), because this link affects our resource in some way
Or to say it in code:
$user.update(data).then(function($updatedUser) {
$user === $updatedUser; // true
});
$user.car().then(function($car) {
$user === $car; // false
});
$user.kill().then(function($killedUser) {
$user === $killedUser; // true, because $user automatically calls and returns $user.self() on $user.kill() internally
});