<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.rawgit.com/js-data/js-data/master/dist/js-data.js"></script>
<script src="script.js"></script>
</head>
<body>
<h1>Open up your console to see the output</h1>
</body>
</html>
var store = new JSData.DS({
basePath: 'http://mybaseapp.com/api'
});
var Document = store.defineResource({
name: 'document',
idAttribute: '_id',
endpoint: '/documents',
basePath: 'http://myoverridingapp.com/api',
beforeDestroy: function(resourceName, attrs, cb) {
console.log('looks good to me');
cb(null, attrs);
},
methods: {
sayMyName: function() {
console.log(this.name);
}
}
});
var User = store.defineResource('user');
console.log('store.defaults.basePath', store.defaults.basePath);
console.log('Document.basePath', Document.basePath);
console.log('User.basePath', User.basePath);
var doc = Document.inject({
_id: 1,
name: 'myFile'
});
console.log('Document.inject({ _id: 1, name: \'myFile\' })', doc);
doc.sayMyName();
console.log('Document[Document.class].toString()', Document[Document.class].toString());
console.log('doc.constructor', doc.constructor);
console.log('Document.is(doc)', Document.is(doc));
console.log('store.is(\'document\', doc)', store.is('document', doc));
## DS#defineResource(definition)
Define a resource and register it with the data store.
Returns the new resource.
###### Options
Only some of the available options are listed here. See http://www.js-data.io/docs/dsdefaults for all options.
| 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. 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 to 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 of 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 you 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)](/docs/custom-instance-behavior). |
| computed | object | See [Computed Properties](/docs/computed-properties). |
| beforeValidate | function | See [Model Lifecycle Hooks](/docs/model-lifecycle). |
| validate | function | See [Model Lifecycle Hooks](/docs/model-lifecycle). |
| afterValidate | function | See [Model Lifecycle Hooks](/docs/model-lifecycle). |
| beforeCreate | function | See [Model Lifecycle Hooks](/docs/model-lifecycle). |
| afterCreate | function | See [Model Lifecycle Hooks](/docs/model-lifecycle). |
| beforeUpdate | function | See [Model Lifecycle Hooks](/docs/model-lifecycle). |
| afterUpdate | function | See [Model Lifecycle Hooks](/docs/model-lifecycle). |
| beforeDestroy | function | See [Model Lifecycle Hooks](/docs/model-lifecycle). |
| afterDestroy | function | See [Model Lifecycle Hooks](/docs/model-lifecycle). |
| beforeInject | function | See [Model Lifecycle Hooks](/docs/model-lifecycle). |
| afterInject | function | See [Model Lifecycle Hooks](/docs/model-lifecycle). |
| relations | object | See [Relations](/docs/relations). |
###### Examples
```js
var Document = store.defineResource({
name: 'document',
idAttribute: '_id',
endpoint: '/documents',
basePath: 'http://myapp.com/api',
beforeDestroy: function (resourceName, attrs, cb) {
console.log('looks good to me');
cb(null, attrs);
},
methods: {
sayMyName: function () {
console.log(this.name);
}
}
});
var document = Document.inject({ _id: 1, name: 'myFile' });
document.sayMyName(); // "myFile"
```