如何使用Backbone.js的,但在无RESTful方式? [英] how to use Backbone.js, but in the NO-RESTful way?
问题描述
我现在是一个前端开发人员,我有一个项目,这是很好的使用BackboneJS和服务器端是别人写的。是否有任何人谁可以告诉我如何覆盖删除,更新,以不RESTful方式添加,等等?服务器端的URL可能是这样的:
I'm now a front-end developer, and I have a project which is fine to use BackboneJS, and the server-side is written by others. Is there anyone who can tell me how to override delete, update, add, and so on in a not-RESTful way? The server-side's URL may be like this:
- 添加:
www.domain.com/addBookById
- 删除:
www.domain.com/removeBookById
- add:
www.domain.com/addBookById
- remove:
www.domain.com/removeBookById
非常感谢!
推荐答案
骨干网使用 Backbone.sync
管理与服务器的所有通信。大约有同步两个重要的事情
你;首先,它看起来像这样:
Backbone uses Backbone.sync
to manage all communication with the server. There are two important things about sync
for you; first of all, it looks like this:
的方法签名的 Backbone.sync 是同步(方法,模型,[选项])
- 法 - 的CRUD方法(
创造
,读
,更新
或删除
) - 模式 - 该模型被保存(或读取集合)
- 选项 - 成功和错误回调,和所有其他的jQuery请求选项
- method – the CRUD method (
"create"
,"read"
,"update"
, or"delete"
) - model – the model to be saved (or collection to be read)
- options – success and error callbacks, and all other jQuery request options
和第二个是,你可以覆盖同步
在每个模式和每个收集的基础。所以,你可以在自己的同步
实现添加到模型:
and the second is that you can override sync
on a per-model and per-collection basis. So you can add your own sync
implementation to your model:
var M = Backbone.Model.extend({
sync: function(method, model, options) {
//...
},
//...
});
如果你看看法
您可以决定使用哪个URL,以及是否你正在做一个GET,POST,......请求。在模式
会告诉你什么样的数据发送到服务器。您将要合并选项
成你想要使用 $。阿贾克斯
选项。看看标准的实施 Backbone.sync
,它是pretty直线前进,应该告诉你,你需要做的:只需更换URL处理和降一些功能,你不关心(如 emulateHTTP
和 emulateJSON
)。
If you look at method
you can decide which URL to use and whether you're doing a GET, POST, ... request. The model
will tell you what data to send to the server. You'll want to merge options
into the $.ajax
options you want to use. Have a look at the standard implementation of Backbone.sync
, it is pretty straight forward and should show you what you need to do: just replace the URL handling and drop some of the features you don't care about (such as emulateHTTP
and emulateJSON
).
这篇关于如何使用Backbone.js的,但在无RESTful方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!