Backbone.js 本地存储,预先从服务器加载

发布于 2024-12-19 19:55:28 字数 330 浏览 0 评论 0原文

我正在尝试使用backbone.js构建一个移动应用程序,它将:

  1. 在第一次运行时从服务器加载项目列表
  2. 将这些项目提交到本地存储(我正在使用backbone-localstorage.js)
  3. 能够保存列表项目作为收藏夹,将其保存在本地存储中

该应用程序永远不会将数据更改提交到服务器,而仅使用本地存储。

我正在手动从服务器加载数据并填充集合,一切正常,只是我无法将数据传输到本地存储。

我正在用头撞墙,试图让它发挥作用。任何有关如何解决此问题的想法都将受到高度赞赏。我见过的所有示例要么提交到服务器,要么是纯粹的本地存储。

I'm trying to build an mobile application using backbone.js that will:

  1. Load a list of items from the server on its first run
  2. Commit these to Local Storage (I'm using backbone-localstorage.js)
  3. Be able to save list items as favorites, saving this in local storage

The app will never commit changes of the data to the server but only work with the local storage.

I'm loading the data from the server manually and populate the collection, it all works fine only that I can't transfer the data to local storage.

I'm banging my head against the wall trying to get this to work. Any ideas of how to solve this is highly appreciated. All examples I've seen either commits to the server or is pure local storage.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡笑忘祈一世凡恋 2024-12-26 19:55:28

一些示例代码会很有用。

如果您已经使本地存储正常工作,我不确定您在第一次运行时如何从服务器加载项目列表,但是,这就是我可能将其组合在一起的方式。

如果您在应用程序运行期间不需要返回服务器刷新任何模型,那么最简单的方法就是从应用程序中嵌入的服务器传递模型,并在创建集合时加载它们:

foo = new MyCollection(myJSONthatContainsAllTheModels)

有关详细信息,请参阅 Backbone 文档中的引导

如果您需要在应用程序运行过程中定期从服务器刷新,那么..

使用backbone-localstorage就像,只需进行一次更改。这意味着它将覆盖同步方法,任何正常的“获取”、“保存”等功能都将操作本地存储。更改是将原始 Backbone.Sync 复制到一个名为 Backbone.ServerSync 之类的新函数,然后用 LocalStorage 版本替换 Backbone.Sync。这会保留 REST 同步。

然后,我将用一个新函数扩展 Backbone.Collection,该函数用于使用 Backbone.ServerSync 函数从服务器获取数据。

看起来backbone-localstorage 不会干扰集合的URL 属性,因此您应该能够很轻松地做到这一点。

这个新的集合函数(为清楚起见称为 serverfetch)或多或少是 fetch 的克隆,但使用 Backbone.ServerSync 方法而不是 Backbone.Sync。

从服务器获取响应并解析它后,它将为每个模型调用 add ,该模型应将它们提交到本地存储。

A little example code would be useful.

I'm not sure how you're loading a list of items from the server on the first run, if you've gotten the local storage working, but, this is the way I'd probably put it together.

The VERY EASIEST way to do this, if you never need to go back to the server to refresh any models during the app's run, is to simply deliver the models from the server embedded in the app, and load them when you create your collection:

foo = new MyCollection(myJSONthatContainsAllTheModels)

For more info, see Bootstrapping in the Backbone Docs.

If you need to, over the course of the app's run, refresh from the server periodically, then..

Use backbone-localstorage is as, with a single change. Which means that its going to override the sync method and any normal "fetch", "save", etc. functions will be manipulating local storage. The change would be to copy the original Backbone.Sync to a new function called something like Backbone.ServerSync before replacing Backbone.Sync with the LocalStorage version. This preserves the REST sync.

Then, I would extend Backbone.Collection with a new function which is used to fetch data from the server, using your Backbone.ServerSync function.

It doesn't look like backbone-localstorage interferes with the URL properties of collections, so you should be able to do this pretty easily.

This new collection function, called, say, serverfetch for clarity would more or less be a clone of fetch, but using your Backbone.ServerSync method instead of Backbone.Sync.

After it gets the response from the server and parses it, it will call add for each model which should commit them to local storage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文