如何将backbone.sync与jstorage集成?
我是一名 javascript 开发新手(这里是第一篇文章!),最近一直在尝试使用backbone.sync。我一直在阅读 todo 示例,并注意到它使用 < a href="http://documentcloud.github.com/backbone/docs/backbone-localstorage.html" rel="nofollow">backbone-localstorage。我的感觉是,backbone-localstorage 只是作者用于演示目的的快速实现。我还注意到密钥是随机生成的,而我想要一些可以让我命名自己的密钥值的东西。
我一直在研究 jstorage (www.jstorage.info),并且非常感谢一些关于如何将其与backbone.js 集成的指示(最好带有代码示例)。我想 backbone.sync 需要以某种方式被覆盖,就像在骨干本地存储中所做的那样。
或者,您会推荐什么 localStorage 插件/包装器,以及它将如何与主干集成?
提前感谢您的帮助。
I'm a newbie javascript developer (first post here!) and been recently trying to play with backbone.sync. I've been reading through the todo example and notice that it uses backbone-localstorage. My feeling is that backbone-localstorage was just a quick implementation that the author used for demo purposes. I also noticed that the keys are randomly generated, whereas I would like something that would allow me to name my own key values.
I've been looking at jstorage (www.jstorage.info), and would really appreciate some pointers (preferably with code samples) on how to integrate it with backbone.js. I imagine backbone.sync would need to be overriden somehow as is done in backbone-localstorage.
Alternatively, what localStorage plugin/wrapper would you recommend, and how would it integrate with backbone?
Thanks for help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完全正确。我想说,这种实现对于大多数事情来说都很好,但是您可以获得的支持可能很少。
这其实并不正确。我假设您指的是:
在这里,发生的情况是,当在模型上调用 create 时,它会尝试确定是否已在模型上设置
id
,如果没有这样的id,然后使用
guid
函数来构建一个。这不是随机设置密钥,而是满足每个保存的模型都应该有一个 ID 的要求(通过分配一个随机 ID)。修改
backbone-localstorage.js
代码应该相当简单。让我们以存储构造函数为例这里我们唯一需要更新的是对 localStorage 的调用:
同样简单的是 save 函数:
同样,只需更新对 localStorage 的调用:
更新: jStorage 句柄为您提供所有 JSON.stringify 和 JSON.parse,因此我更新了上面的代码以反映这一点。
简单易行,对吧!
无论如何,这是一次很棒的练习,祝你好运。我希望我能够帮助你。
Ps localStorage 插件让我困扰的一件事是它使用
model.attributes.xxx
,其中最好使用model.get("xxx")
。这样,如果他们更改了模型更改的内部结构,您的代码就不会中断。至于如何生成指南以及随机指南是否适合您,取决于您的领域。对于 TODO 示例,没有可同步的后备数据库,因此任何旧的 guid 都可以解决问题,但在许多其他情况下,您可能会使用 html5 存储来创建应用程序的离线版本,因此 id 需要兼容与数据库。在这些情况下,使用随机 guid 可能不是一个好主意,但是骨干本地存储插件提供的 guid 的设计是为了它不太可能与数据库的实际 id 发生冲突,所以它不是 _太糟糕了。这里并没有真正正确的答案,所以请做对您的领域有意义的事情。
Exactly right. I'd say that the implementation is fine for most things, but what support you can get for it is probably minimal.
This isn't really correct. I assume you are referring to this:
Here, what's happening is that when a create is called on a model, it tries to determine if an
id
has been set on the model, and if no suchid
is provided, then theguid
function is used to build one. This isn't setting a key randomly, it's fulfilling the requirement that every saved model should have an id (by assigining a random one).Modifying the
backbone-localstorage.js
code should be fairly trivial. Let's look at the store constructor as an exampleThe only thing we need to update here is the call to localStorage:
Similarly simple is the save function:
again, simply update the call to localStorage:
Update: jStorage handles all the JSON.stringify, and JSON.parse for you, so I've updated the above code to reflect that.
Easy peasy, right!
Anyways, this is a great exercise, and I wish you the best of luck. I hope that I've been able to help you out.
P.s. One thing that bothers me about the localStorage plugin is that it uses
model.attributes.xxx
, where it's better to usemodel.get("xxx")
. This way, if they ever change the internals of model change, your code won't break.P.p.s. as far as how to generate guids, and weather a random guid is appropriate for you, depends upon your domain. With the TODO example, there is no backing database to sync with, so any old guid would do the trick, but in many other cases, you may be using html5 storage to create an offline version of your app, so id's need to be compatable with the db. In these cases, using a random guid is probably not that great an idea, but the one provided by the backbone-localstorage plugin is designed so that it isn't likely to collide with your db's actual id's, so it's not _that bad. There isn't really a right answer here, so do what makes sense for your domain.
您可能应该首先查看backbone.js 提供的本地存储插件。有大量代码示例可以让您了解如何将 jstorage 替换为底层存储机制。在这里查看:
backbone-localstorage.js
You should probably look first at the local storage plugin that backbone.js provides. There are plenty of code samples that should give you an idea of how you would swap in jstorage as the underlying storage mechanism. See it here:
backbone-localstorage.js