Backbone.js able to do rest and localstorage?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 00:29:39

问题


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 therefore stops backbone from pushing to the server(?). Ideally, I would like pass my data back to the server as well and persist it locally when online and just use localstorage when offline (you know, the perfect app). I haven't found any documentation yet.

Is Backbone.localStorage a part of this? Has anyone been able to build this scenario? How is this done? (Please tell me I don't have to roll my own.)

Thanks.


回答1:


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);
};


来源:https://stackoverflow.com/questions/5761349/backbone-js-able-to-do-rest-and-localstorage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!