是否有任何 Backbone.js 教程教授“.sync”与服务器?
我读了很多 Backbone.js 教程,但其中大多数都涉及静态对象。
当然,我在服务器上有数据。我想要一个教程来展示backbone.js如何与服务器通信以获取数据、发布数据等。
这是.sync
,对吧?我阅读了backbone.js文档,但对如何使用这个功能仍然很模糊。
或者有人可以给我举个例子吗?
根据:http://documentcloud.github.com/backbone/#Sync
Backbone.sync是Backbone每次调用时调用的函数 尝试读取模型或将模型保存到服务器。
但什么时候呢?我将该函数放在哪里?我不知道如何使用它,文档也没有给出任何示例。数据何时加载到我的模型中?我要定义什么时候......对吧?
I read many Backbone.js tutorials, but most of them deal with static objects.
Of course, I have data on the server. I want a tutorial that shows how backbone.js can communicate with the server to fetch data, post data, etc.
This is .sync
, right? I read the backbone.js documentation, but still fuzzy on how to use this feature.
Or can someone show me an example?
According to: http://documentcloud.github.com/backbone/#Sync
Backbone.sync is the function that Backbone calls every time it
attempts to read or save a model to the server.
But when? Where do I put the function? I don't know how to use it, and the documentation doesn't give any examples. When does the data get loaded into my models? I get to define when...right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您永远不必真正查看 .sync,除非您打算覆盖它。对于正常使用,您可以随时调用 model.save() ,这将执行 post 或 put (取决于记录是否已存在)。如果您想从后端获取数据,请使用 collection.fetch()
您当然还需要指定一个 URL,通过您的集合属性 collection.url 来执行此操作
You never really have to look at .sync, unless you plan to overwrite it. For normal uses, you can simply call model.save() whenever you want and that will execute a post or put (depending on whether the record exists already). If you want to get the data from your backend, use collection.fetch()
You'll of course also need to specify a URL, do so through your collection attribute, collection.url
如果您覆盖它,则可以覆盖 Backbones 本机同步功能:
此后,每当您调用主干函数(例如模型上的 .save() 或集合上的 .fetch())时,都会调用此函数。您不必再关心数据传输。
我建议查看 Backbones 源代码并了解默认同步功能是如何实现的。然后创建您自己的或采用您的服务器来支持本机功能。
You can override Backbones native sync functionality if you override it:
After that this function is called whenever you call a backbone function like .save() on models or .fetch() on collections. You do not have to care about data transport anymore.
I would suggest taking a look into Backbones source and look how the default sync function is implemented. Then create your own or adopt your server to support the native function.
它们不是免费的,但以下截屏视频都有关于后端工作以及如何向 Backbone 发送数据和从 Backbone 获取数据的内容。
Tekpub 是关于 asp.net MVC3 的 9 部分截屏视频,整个第 6 部分是关于使用骨干网编写一个管理模块来管理生产。它展示了有关在 MVC3 中处理路由以及发送和发送消息的所有内容。接收数据
Peepcode
They are not free, but the following screencasts both have a piece on backend work and how to send data to and get data from Backbone.
Tekpub is a 9 part screencast about asp.net MVC3, with the whole 6th part about using backbone to write an admin module to manage productions. it shows all about handling routing in MVC3 and sending & receiving data
Peepcode