I have a question about loading and caching remote objects with Ember. I'm developing an Ember app that uses server-side storage through a REST API. Some of the fetched data is rarely changing, so fetching it from the server each time the application loads is unnecessary. But this is also a question for apps that need to work offline and still save its data to a server.
Ember Data has a built-in storage adapter for persisting models through a REST API, and there is an adapter for Local Storage as well (as pointed out by Ken below). The problem (if it is a problem) is that a model only has one storage adapter, and there doesn't seem to be any concept of caching fetched models other than keeping them in memory.
I found similar requests in this Ember wishlist and in the comments to this talk by Tom Dale, but I haven't found any indication that this would be an existing feature in Ember.
I have two questions (the first one being the important one):
- What is the best way – today – to implement cached models in Local Storage and syncing them with remote data as needed?
- Is this a feature that is planned be included in Ember, or at least something that the maintainers feel should be added eventually?
When it comes to 1), I can think of a couple of strategies:
a) Extend an existing adapter and add a custom remote sync mechanism:
App.Store.registerAdapter('App.Post', DS.LSAdapter.extend({
// do stuff when stuff happens
}));
b) Maintain separate model classes – one set for the remote objects, and one set for local objects – and sync between them as needed. With the standard Todo case:
RemoteTodo –*sync*– Todo
|
UI
I'm kinda hoping this is a real noob question and that there is a good established pattern for this.
Updated: Found this similar question. It has a good answer, but it's kind of theoretical. I think what I would need is some hands-on tips or pointers to example implementations.
question from:
https://stackoverflow.com/questions/14814472/caching-remote-data-in-local-storage-with-emberdata 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…