<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-data/2.5.0/js-data.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-data-http/2.1.1/js-data-http.min.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({
actions: {
findOne: {
response: function (data) {
console.log('response', arguments);
return data;
}
}
}
});
store.registerAdapter('http', new DSHttpAdapter(), { default: true });
var Document = store.defineResource('document');
var _doc;
console.log('Document.find(1)');
Document.find(1).then(function(document) {
_doc = document;
console.log('document', document);
console.log('Document.find(1)');
return Document.find(1);
}).then(function(document) {
console.log('no new request was made');
console.log('resolving with the cached item');
console.log('document', document);
console.log('_doc === document', _doc === document);
});
## DS#find(resourceName, id[, options])
The "R" in "CRUD", `find` is for retrieving a single item via an adapter. With completely default settings, it's probably going to end up making a GET request to /resource/:id, where `resource` is the name of the resource and `:id` is the primary key of the item to retrieve, e.g. `store.find('user', 1)`. If you're working with the resource directly you can just do `User.find(1)`.
`find` is asynchronous and returns a promise.
`find` first checks to see if the item is already in the store and if so immediately resolved the promise with the item. Otherwise, it delegates to the `find` method of whichever adapter is being used and then injects the resulting item into the data store.
`bypassCache: true` will force delegation to the adapter (ignore the item in the store).
`cacheResponse: false` will cause the result to _not_ be injected into the store.
If `find` ends up delegating to an adapter, the `options` argument (if you passed one) will also be passed to the adapter's `find` method, so you can pass options to the adapter as well.
If the result is to be injected into the store, the `options` argument will also be passed to [`DS#inject`](http://www.js-data.io/docs/dsinject) when it is called.
{ "id": 1 }