new Resource(service, name, type, schema, value, $states, components, endpointBase, relationName)
Apy Resource Component
Parameters:
| Name | Type | Description |
|---|---|---|
service |
apy.CompositeService | Service instance |
name |
string | Resource name |
type |
string | Resource type |
schema |
Object | Resource schema |
value |
Object | Resource value |
$states |
apy.helpers.StateHolder | Resource inner state holder instance |
components |
Array | Resource initial components |
endpointBase |
string | Resource endpoint |
relationName |
string | (optional) Resource relation name |
Example
var endpoint = 'http://localhost:5000/';
// Schema (usually it comes from REST API or configuration, not that way)
var post = {
item_title: "Post",
schema: {
description: {
type: "string"
},
title: {
type: "string"
}
}
};
// Associated data (usually, data is bound to some UI framework)
var postData = {
title: "A nice title",
description: "And another nice description..."
};
// Service is usually initialized by UI framework in charge of representing data
var service = new apy.CompositeService(...);
var resource = new apy.components.Resource(service, post.item_title, post.schema, postData, null, endpoint);
function error(e) {
console.log('CREATE Error', e);
}
function success(response) {
if(!response) console.log('Nothing to create');
else console.log('CREATE ', response);
}
resource.create()
.then(success)
.catch(error);
Extends
Methods
-
<static> create()
-
If the Resource is created & updated, a POST Request is sent to the backend.
Returns:
Asynchronous call
- Type
- Promise
-
<static> createResourceRequest(method)
-
Wraps common
createRequestmethod adding some specific Resource logic.On success, the Resource inner (meta-)data are automatically updated and its state restored to READ while passing the response as-is.
On failure, error is logged and passed as-is.
Parameters:
Name Type Description methodstring The HTTP Method Verb (GET, POST, DELETE, ...)
Returns:
Asynchronous call
- Type
- Promise
-
<static> hasCreated()
-
Allow to know if the Resource is a fresh created Resource or loaded one (_id ? hasCreated : not)
Returns:
Has the Resource been created ?
- Type
- boolean
-
<static> update()
-
If the Resource is not created & updated, a PATCH Request is sent to the backend.
Returns:
Asynchronous call
- Type
- Promise
-
delete()
-
If the Resource is not created, a DELETE Request is sent to the backend.
Returns:
Asynchronous call
- Type
- Promise