4 ms·
I wrote something very similar a week ago: https://github.com/scosman/Backbone.CachedModel https://github.com/scosman/Backbone.CachedModel Differences: - it w
by scosman 15y ago
I wrote something very similar a week ago: https://github.com/scosman/Backbone.CachedModel https://github.com/scosman/Backbone.CachedModel
Differences:
- it works with any .sync implementation you might already have (JSON, XML, whatever). It just drops in, caching the server responses locally, not assuming anything about how the service works
- instead of overridding Backbone.Sync I let you extend CachedModel on a per model basis (that way some models can be cached locally while others are not)
I didn't include any fancy cache expiry, just using a simple localstorage.clear on login for now.
example usage:
var User = Backbone.CachedModel.extend({
cacheName: 'USERS',
});
- lucian1900 15y agoInteresting. I may steal some code :) I handle not sending models to the server with remote/local boolean attributes on the model. Certainly not as well decoupled as yours.
- scosman 15y agoanother thing to consider, Store.records is keeping everything in memory. Using only localstorage should save some memory (although it depends on the browser implementation)
- lucian1900 15y agoGood point. On the other hand, localStorage access appears to block, sometimes quite a bit. I'll investigate some more. It's a bit I inherited from Backbone.localStorage, and haven't looked into much.