Refactor notifications localStorage cache into adapter pattern.

Sometimes you want stale data right away, then refresh it async.
This adds `findStale` to the store for that case. If it returns
an object with `hasResults` you can get the `results` and display
them.

It also returns a `refresh()` method to freshen up the stale data.

To enable `localStorage` support for stale data, just include the
mixin `StaleLocalStorage` into an adapter for that model. This
commit includes a sample of doing that for `Notifications`.
This commit is contained in:
Robin Ward
2015-08-31 14:09:57 -04:00
parent b8c3187a94
commit ddf0db0338
10 changed files with 113 additions and 47 deletions

View File

@ -63,6 +63,17 @@ test('find with query param', function() {
});
});
test('findStale with no stale results', (assert) => {
const store = createStore();
const stale = store.findStale('widget', {name: 'Trout Lure'});
assert.ok(!stale.hasResults, 'there are no stale results');
assert.ok(!stale.results, 'results are present');
return stale.refresh().then(function(w) {
assert.equal(w.get('firstObject.id'), 123, 'a `refresh()` method provides results for stale');
});
});
test('update', function() {
const store = createStore();
return store.update('widget', 123, {name: 'hello'}).then(function(result) {
@ -134,3 +145,4 @@ test('findAll embedded', function(assert) {
assert.equal(fruits.objectAt(2).get('farmer.name'), 'Luke Skywalker');
});
});