Backbone.js able to do rest and localstorage?

后端 未结 1 642
臣服心动
臣服心动 2021-01-29 23:22

I have been experimenting with the localstorage module for Backbone.js (https://github.com/jeromegn/Backbone.localStorage). As I understand it this overloads Backbone.sync and t

相关标签:
1条回答
  • 2021-01-30 00:18

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

    You can use simple feature detection for whether the user is offline or online and then asynchronously load Backbone.localStorage.js if they are offline.

    If neccesary you can also pass in a specific version of Backbone.sync to your models and collections.

    If you want to do both at the same time you'll have to write your own version of Backbone.sync that both calls the server and calls localStorage.

    The easiest way to do this is to just define

    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 then you can do the following

    Backbone.sync = function Sync() {
        Backbone.ajaxSync.apply(this, arguments);
        return Backbone.localSync.apply(this, arguments);
    };
    
    0 讨论(0)
提交回复
热议问题