Can Backbone.js have a rest and localstorage?

I experimented with the localstorage module for Backbone.js (https://github.com/jeromegn/Backbone.localStorage). As I understand it, this overloads Backbone.sync and therefore stops the trunk from clicking on the server (?). Ideally, I would like to transfer my data back to the server, and also save it locally when it is online, and just use localstorage when it is offline (you know, an ideal application). I have not found any documentation yet.

Is Backbone.localStorage part of this? Has anyone been able to build this scenario? How it's done? (Please tell me that I do not need to minimize.)

Thank.

+42
javascript local-storage offline
Apr 23 2018-11-11T00:
source share
1 answer

Backbone.localStorage is an external file that you can use that overwrites Backbone.Sync.

You can use simple function detection, whether the user is offline or online, and then download Backbone.localStorage.js asynchronously if they are offline.

If necessary, you can also transfer a specific version of Backbone.sync your models and collections.

If you want to do both at the same time, you will have to write your own version of Backbone.sync, which calls the server and calls localStorage.

The easiest way to do this is to simply determine

 Backbone.sync = function() { originalSync.apply(this, arguments); localStorageSync.apply(this, arguments); } 

Edit:

As mentioned in the comments, if you use the latest backbone localStorage plugin , you can do the following

 Backbone.sync = function Sync() { Backbone.ajaxSync.apply(this, arguments); return Backbone.localSync.apply(this, arguments); }; 
+45
Apr 23
source share



All Articles