DS#defineResource
DS#defineResource(options)
Define a resource and register it with the data store. Takes just a single argument: a hash of options (object). As a convenient shorthand, you can just pass the name (string) if you don't need to specify any options.
Returns the new resource.
var store = new JSData.DS();
// DS#defineResource takes a single argument: a hash of options
var Document = store.defineResource({
name: 'document',
endpoint: 'documents'
});
// convenient shorthand when you don't need to specify any options
// just pass the name
var User = store.defineResource('user');
Note!
The resource definition can override any option configured on the whole data store. Here is the complete list of data store options: Configuration
Options
Only some of the available options are listed here. See the note directly above.
name | type | description |
---|---|---|
name | string | Required. The name of the new resource. |
idAttribute | string | The name of the field to use as the primary key for instances of this resource. Computed properties are supported as ids. Default: "id" . |
basePath | string | Override the default basePath for this resource. Default: DS#defaults.basePath |
endpoint | string | Override the default endpoint for this resource. Default: name . |
useClass | boolean | Whether to use a wrapper class created from the ProperCase name of the resource. Must be true for computed properties and instance methods to work. Default: true . |
keepChangeHistory | boolean | Whether to keep a history of changes for items in the data store. Default: false . |
resetHistoryOnInject | boolean | Whether to reset the history of changes for items when they are injected or re-injected into the data store. This will also reset an item's previous attributes. Default: true . |
defaultFilter | function | Override the filtering used internally by DS.filter with your own function here. Default: See the source code. |
meta | object | Put anything you want here. It will never be used by the API. |
methods | object | See Instance Methods (Custom instance behavior). |
actions | object | See Configuration (actions). |
computed | object | See Computed Properties. |
beforeValidate | function | See Model Lifecycle Hooks. |
validate | function | See Model Lifecycle Hooks. |
afterValidate | function | See Model Lifecycle Hooks. |
beforeCreate | function | See Model Lifecycle Hooks. |
afterCreate | function | See Model Lifecycle Hooks. |
beforeUpdate | function | See Model Lifecycle Hooks. |
afterUpdate | function | See Model Lifecycle Hooks. |
beforeDestroy | function | See Model Lifecycle Hooks. |
afterDestroy | function | See Model Lifecycle Hooks. |
beforeInject | function | See Model Lifecycle Hooks. |
afterInject | function | See Model Lifecycle Hooks. |
relations | object | See Relations. |
Live Demo
Additional reading:
Need help?
Want more examples or have a question? Ask on the Slack channel or post on the mailing list then we'll get your question answered and probably update this wiki.
Updated less than a minute ago