开始使用backbonejs - 服务器应该返回什么 [英] Getting started with backbonejs - what should a server return

查看:20
本文介绍了开始使用backbonejs - 服务器应该返回什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Backbone.js 库完全陌生,并通读了整个文档并了解了该库的工作原理.在以下情况下,服务器的响应应该如何才能使使用主干设计的应用程序正常工作(无需添加额外的笔画/代码).

I am totally new to Backbone.js library and read through the whole documentation and understood the working of the library. In the below cases what should the response from the server be for proper working of application designed using backbone (without putting a extra stroke/code).

假设一个模型如下

window.person = Backbone.Model.extend({
    defaults: {
        name: "",
        email: "dummy@gmail.com"
    },
    urlRoot: "PersonApp"
});

  1. 假设 model.save()

服务器应该为 model.fetch()

服务器应该为 model.destroy()

推荐答案

如果您查看 Backbone.Sync 文档,它说您应该使用服务器上已更改的属性响应请求.

If you have a look in the Backbone.Sync documentation, it says that you should respond to requests with the attributes that have changed on the server.

所以回答你的问题:

  1. model.save 的 JSON 请求应返回作为保存一部分已更改的属性.在创建的情况下,这将是整个模型;在仅更新已更改的字段的情况下.(或者,如果您很懒惰并且不介意更新整个客户端模型,您可以只返回整个模型).

  1. The JSON request for model.save should return the attributes that have changed as part of the save. In the case of a create this would be the whole model; in the case of update just the fields that have changed. (Or if you're lazy and don't mind updating the entire client side model, you could just return the whole model).

所以可接受的响应是 { 'name' : 'a name', 'email' : 'an@email.com' }

Fetch 应该只以 JSON 形式返回模型.因此,我用于 model.save 的相同示例也可以使用.

Fetch should just return the model in JSON form. So, the same example I used for model.save would work.

我不完全确定,但我认为 Backbone 不会验证从删除请求返回的数据,因此您应该能够返回任何内容,只要它不是 HTTP 错误.根据@a.real.human.being 下面的说法,空响应也会导致错误.所以用OK"返回 200在正文(或类似的)中似乎是一个合理的计划.

I'm not entirely sure, but I don't think Backbone validates the returned data from delete requests so you should be able to return anything, so long as it's not an HTTP error. According to @a.real.human.being below, an empty response also causes errors. So returning a 200 with "OK" in the body (or similar) seems like a reasonable plan.

这篇关于开始使用backbonejs - 服务器应该返回什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆