javascript Backbone.js 能够做休息和本地存储吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5761349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:15:39  来源:igfitidea点击:

Backbone.js able to do rest and localstorage?

javascriptlocal-storageofflinebackbone.js

提问by LeRoy

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.

我一直在试验 Backbone.js (https://github.com/jeromegn/Backbone.localStorage) 的 localstorage 模块。据我了解,这会使 Backbone.sync 过载,因此会阻止主干推送到服务器(?)。理想情况下,我也希望将我的数据传回服务器,在线时将其保存在本地,离线时仅使用 localstorage(您知道,完美的应用程序)。我还没有找到任何文档。

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.)

Backbone.localStorage 是其中的一部分吗?有没有人能够构建这个场景?这是怎么做的?(请告诉我我不必自己动手。)

Thanks.

谢谢。

回答by Raynos

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

Backbone.localStorage 是一个外部文件,您可以使用它覆盖 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.

您可以使用简单的功能检测用户是离线还是在线,然后在离线时异步加载 Backbone.localStorage.js。

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

如果需要,您还可以将特定版本传递Backbone.sync给您的模型和集合。

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.

如果您想同时执行这两项操作,则必须编写自己的 Backbone.sync 版本,该版本既调用服务器又调用 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 pluginthen you can do the following

如评论中所述,如果您使用最新的主干localStorage 插件,那么您可以执行以下操作

Backbone.sync = function Sync() {
    Backbone.ajaxSync.apply(this, arguments);
    return Backbone.localSync.apply(this, arguments);
};