Constructor
new DataObject(optionsopt)
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
options |
Object |
<optional> |
{} | Not used by DataObject but helpful for extending classes |
- Source:
Members
cleanedUp
True after DataObject.cleanup has been called.
- Source:
fetchOptions
Extending classes can override this to add headers, methods, etc to the fetch call
By default the only fetch option set is `credentials: same-origin'.
- Source:
isNew
True until a fetch (even a failed fetch) returns
- Source:
options :Object.<string, *>
Type:
- Object.<string, *>
- Source:
url
The URL (relative or full) as a string for the endpoint used by DataObject.fetch.
- Source:
Methods
cleanup() → {DataObject}
When models are no longer needed `cleanup` should be called to release resources like event listeners.
- Source:
Returns:
returns `this` for easy chaining
- Type
- DataObject
delete() → {Promise.<undefined, Error>}
Call `DELETE` on this object's remote endpoint.
- Source:
Returns:
- Type
- Promise.<undefined, Error>
equals()
Extending classes can override this to allow less strict equality
- Source:
fetch() → {Promise.<DataObject, Error>}
Ask the server for data for this model or collection.
Depends on DataObject.url, DataObject.parse and DataObject.reset.
- Source:
Returns:
- Type
- Promise.<DataObject, Error>
onFirstReset(callback)
If already reset, immediately call callback, otherwise wait until the first reset and then call callback
Parameters:
Name | Type | Description |
---|---|---|
callback |
DataObject~resetCallback |
- Source:
Example
class ExampleModel extends DataModel {
get url() { return '/api/example' }
}
const model = new ExampleModel()
model.onFirstReset((model) => { ... })
model.fetch()
// the callback passed to onFirstReset will be called when the fetch completes
parse()
Extending classes can override this to parse the data received via a fetch
- Source:
reset()
Clear out old data and set it to data, should trigger a 'reset' event
- Source:
save() → {Promise.<DataObject, Error>}
Tell the server to create (POST) or update (PUT) this model or collection.
If DataObject.isNew is true it will POST, otherwise it will PUT.
- Source:
Returns:
- Type
- Promise.<DataObject, Error>
stringify() → {string}
- Source:
Returns:
Extending classes must return a JSON-formatted version of their data
- Type
- string
(static) fetchAll(…dataObjects) → {Promise.<...DataObjects>|Error}
Fetch each DataObject and then wait for them all to return
This resolves when the fetches complete, regardless of whether they succeed or fail.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
dataObjects |
DataObject |
<repeatable> |
- Source:
Returns:
- Type
- Promise.<...DataObjects> | Error
Example
this.model = new DataModel()
this.collection = new DataCollection()
DataObject.fetchAll(model, collection).then((mod, col) => { ... })
Type Definitions
resetCallback(dataObject)
Parameters:
Name | Type | Description |
---|---|---|
dataObject |
DataObject |
- Source: