An unobtrusive Backbone cache
I did some research last night to save myself the trouble of having to implement my own Backbone caching system. I found Backbone.localStorage and Backbone.offline. Backbone.localStorage wouldn’t work for me properly. It wouldn’t do the initial fetch to retrieve and store data, and Backbone.offline looked great but it didn’t suit my needs at all, though I will definitely try it on another project. Boy was I upset. I went back to Backbone.localStorage and read everything, and tried everything I could, but I really couldn’t get it to that initial data store. This time I decided to give up and take a look at it another day.
I still had time to spare so I did search on GitHub for “backbone” and went through every project with backbone in the title to see what people were making, and if I could use any of it. Then, I found Backbone.cachingSync. This plugin doesn’t try to take over Backbone, and it doesn’t operate on the assumption that it knows what the server has, even when it hasn’t made a single server call.
Instead, it stores the results in localStorage and on-fetch it returns the localStorage values, but it also sends a command to the server to see if the information has changed at all. It throws a sync message when it successfully retrieves data, and change if the data has changed just like normal. Which is perfect for my current real-time strategy game. It helps me get the interface render done as quick as possible, while verifying the data and allowing my view to update only if necessary.