I think the "remote sync" label is misleading. It will sync properly if the remote host is accessible. If you create something offline, it will never sync it back to the remote host when the connectivity is restored. It will stay local and might lead to problems down the road (when updating a locally created record, the remote host will not know about it)
Oh, wow! This is awesome; I'm just now at the point in a Backbone project where I was preparing to implement something similar, and wasn't exactly relishing the thought. It's awesome that I now have something to work with instead of having to roll my own.
I do have one suggestion, though: while it's of course not much work to compile the script to JavaScript, it couldn't hurt to include a current JS version in the repo for people who don't have Node & CoffeeScript installed.
- 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',
});
another thing to consider, Store.records is keeping everything in memory. Using only localstorage should save some memory (although it depends on the browser implementation)
If I make changes based on a model that I've read from my local cache ... while someone else edits the same model on the server ... how does that reconcile when I try later on to make a change?
I've started using ShareJS[1] in a new app recently to handle real-time collaboration where conflicts are likely. It's spearheaded by an ex-Google Wave engineer and uses Operational Transformation principles to handle automatic merges.
I've been thinking for a little bit now that it would awesome to build a Backbone sync system that could cache things locally when the user is offline, then seamlessly handle bringing them back in sync with ShareJS.
This is intriguing. I've spent a bit of time building Backbone projects that sync with the server (eg. https://github.com/napoleond/localModel) but until now have always resolved conflicts by choosing the model with the most recent time modified. (I have also played a bit with using WebSockets to keep the models up to date for every concurrent user, although that was mostly just for fun.) It would be way better to actually merge diffs in an intelligent manner, though. Thanks for the link!
My implementation leaves a lot to be desired with regards to re-use (there are certain project requirements which happened to be met by the project I built it for) but it does nicely manage automatic replication of the most recent version of models between server and client.
As far as I can tell, this implementation reads from the local model if it exists, otherwise it loads from the server? And writes always go to the server unless explicitly told otherwise?
This is great, I love web interfaces that are completely async where you can do whatever you want in realtime and it "magically" gets stored on the server for me later.
This just makes loading up a page even faster as the data is more than likely already on your computer.
Will this work for offline? If someone views a todolist, makes some changes offline, then visits it later online will it sync the changes automatically?
Not entirely automatically. The app would have to explicitly sync the model when it believes it can.
I do plan to add a helper for that, that keeps a list of unsynced models. Then a function could be called when the server is reachable, or just periodically.
This helper + the PhoneGap network event monitoring would make this indispensable for a bunch of projects. I know I would love it. Anyway I can 'tip' your work?
This sounds cool. Does the local storage extend to collections? Can I query for locally stored objects? (i.e. created or updated within a given date range)