I'm writing a mobile HTML app in which I want to keep preferences saved for future use. Backbone.js gives you a Model and a Collection class which you can use to persist data over HTTP, but in my case, I have no need for the latter as it'll always be only one instance of it saved containing an arbitrary number of properties.
Backbone models assume you have a Collection class associated with it that has a url
method set on them so it knows where to shoot the HTTP request at. But I want to persist my preferences locally via localStorage. That's doable simply by overriding sync
on the particular class:
MobileSettings = Backbone.Model.extend({
sync: function(method, model, options) {
switch(method) {
case 'create':
case 'update':
localStorage.setItem('Settings', JSON.stringify(model));