<!DOCTYPE html>
<html>
<head>
<script src="//cdn.jsdelivr.net/js-data/2.0.0-rc.1/js-data.min.js"></script>
<script src="//cdn.jsdelivr.net/js-data-http/2.0.0-rc.1/js-data-http.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<h1>Open your console to see the output</h1>
</body>
</html>
var store = new JSData.DS();
var adapter = new DSHttpAdapter({
suffix: '.json'
});
store.registerAdapter('http', adapter, {
default: true
});
var Post = store.defineResource({
name: 'post',
// When calling findAll with the same params again,
// instead filter from the store
useFilter: true
});
var page_one_posts = [];
var page_two_posts = [];
Post.createCollection(page_one_posts, {
orderBy: ['id'],
offset: 0,
limit: 2
});
Post.createCollection(page_two_posts, {
orderBy: ['id'],
offset: 2,
limit: 2
});
console.log('page_one_posts.length', page_one_posts.length);
console.log('page_two_posts.length', page_two_posts.length);
Promise.all([
page_one_posts.fetch(),
// ignore the endpoint option, it's just for this contrived plunker example to work
page_two_posts.fetch(null, { endpoint: '_post' })
]).then(function(results) {
console.log('results[0] === page_one_posts: ', results[0] === page_one_posts);
console.log('results[1] === page_two_posts: ', results[1] === page_two_posts);
console.log('page_one_posts.length: ', page_one_posts.length);
console.log('page_two_posts.length: ', page_two_posts.length);
console.log('page_one_posts: ', JSON.stringify(page_one_posts));
console.log('page_two_posts: ', JSON.stringify(page_two_posts));
// the posts were injected into the store
console.log('Post.getAll().length: ', Post.getAll().length);
console.log('Post.inject({ id: 3 }): ', Post.inject({ id: 3 }));
return Promise.all([
page_one_posts.fetch(),
// ignore the endpoint option, it's just for this contrived plunker example to work
page_two_posts.fetch(null, { endpoint: '_post' })
]);
}).then(function(results) {
console.log('results[0] === page_one_posts: ', results[0] === page_one_posts);
console.log('results[1] === page_two_posts: ', results[1] === page_two_posts);
console.log('page_one_posts.length: ', page_one_posts.length);
console.log('page_two_posts.length: ', page_two_posts.length);
console.log('collections were updated with the right stuff from the store');
console.log('page_one_posts: ', JSON.stringify(page_one_posts));
console.log('page_two_posts: ', JSON.stringify(page_two_posts));
console.log('Post.filter({ offset: 4, orderBy: ["id"] }): ', JSON.stringify(Post.filter({ offset: 4, orderBy: ['id'] })));
// the posts were injected into the store
console.log('Post.getAll().length: ', Post.getAll().length);
});
[{
"id": 1
}, {
"id": 2
}]
[{"id":1},{"id":2}]
[{"id":7},{"id":8}]