New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
puzzled about local storage adapter behaviour #86
Comments
This is actually expected behavior (although possibly not desirable behavior). To explain your two scenarios:
Finally, I'd like to point out that the workflow you seem to want to use may not be the right one. Remember that the LocalStorageAdapter can be used for testing and mocking data, but it's also good for actually storing real data in the local storage, which is why it functions more like a data store than an adapter. If you find that you just want to load mock data for testing purposes, you'll be better off overriding shouldInitializeDatabase and getInitialPayload. export default EmberGraph.LocalStorageAdapter.extend({
shouldInitializeDatabase() {
return true;
},
getInitialPayload() {
return {
persistentToken: [
{ id: 'testPush', token: 'test1' }
]
};
}
}); That code will load the payload returned by |
Thanks for the explanation. I see that pushPayload does not really make sense when dealing with local persistence, as the data is just pushed to the store and not the adapter. However, that sounds like there will be a lot of issues for me on the way. I plan on having quite some data that might just be pushed from the server to the client. So there will never be an ajax request or similar - just data from the server. I need to be able to explicitly tell the storage to just save that record, without doing any extras like assigning its own ids. There is another pattern that is quite common, which are PUT requests. They usually allow to create records with a specific id on the server. In that case the id isn't assigned on the server as well, but provided by the client. |
Are you just looking for a caching layer for your server data? If so there's a few ways you could implement that. One would be to override the EmberGraph.Store.extend({
pushPayload(payload, cache = true) {
if (cache) {
// You might need a serializer here if it's not JSON...
localStorage[`cached_payload_${Date.now()}`] = JSON.stringify(payload);
}
this._super(payload);
}
});
Ember.Application.extend({
ready() {
const store = this.get('store');
const cachedDataKeys = Object.keys(localStorage).filter((key) => key.startsWith('cached_payload_'));
cachedDataKeys.forEach((key) => {
store.pushPayload(JSON.parse(localStorage[key]), false);
delete localStorage[key];
});
}
}); You could always perform the caching in your adapter as well (which would be preferable if possible). I've done something like this before (simplified example). EmberGraph.RESTAdapter.extend({
findRecord(typeKey, id) {
const cacheKey = `record:${typeKey}:${id}`;
if (localStorage[cacheKey]) {
const cacheValue = JSON.parse(localStorage[cacheKey]);
if (cacheValue.timestamp + CACHE_EXPIRATION > Date.now()) {
return Promise.resolve(cacheValue.payload);
}
}
const url = this.buildUrl(typeKey, id);
return this.ajax(url, 'GET').then((payload) => {
localStorage[cacheKey] = JSON.stringify({
timestamp: Date.now(),
payload
});
return this.deserialize(payload, { requestType: 'findRecord', recordType: typeKey, id });
});
}
}); Hopefully that makes sense? If not let me know. I've done stuff like this before with both Ember Graph and Ember Data, so I'm sure there's a way to do what you need. |
I have created a few test cases to find out how the local storage adapter works:
I also tried creating a twiddle, but failed with a Namespace error in ember-graph, so here are the classes:
adapters/persistent-token.js:
models/persistent-token.js:
If I call in an action in route/application.js:
and then:
The token is retrieved as expected
If I reload the page and call the above function to retrieve the token again, I receive the following error:
I do not see any entry in chrome's local storage resource. And the data is lost after a reload of the page
Trying to use the save method to persist the token:
and retrieving it with a similar function:
The token is not found. Output is:
However, an entry to the local storage resource has been added. A find all shows that the id has been automatically assigned, even though one had been provided:
leads to output:
If the page is reloaded, the token still is there. However, it is difficult to remember an automatically assigned id between reloads and I think this mainly defeats the purpose.
Am I doing anything wrong, or are these bugs?
The text was updated successfully, but these errors were encountered: