RESTful Backbone应用程序中JSON的预期结构是什么?

时间:2013-09-08 16:33:50

标签: javascript json backbone.js marionette

使用Backbone / Marionette开发专有的CRM应用程序,我想知道,Backbone对JSON的期望是什么结构?这就是我目前正在使用的内容,但我可以将其更改为最适合的内容。 (如果可能的话,我想避免设置自定义解析功能。)

{
    "rowCount":"1",
    "records":[
        {
            "objectName":"",
            "User_UserID":"",
            "User_EmailAddress":"",
            "User_Password":"",
            "User_EncPassword":"",
            "User_Premium":"",
            "User_FirstName":"",
            "User_LastName":"",
            "User_Honorific":"",
            "User_Title":"",
            "User_Nickname":"",
            "User_ParentCompany":"",
            "User_Company":"",
            "User_Publication":"",
            "User_Website":"",
            "User_ShipAddress1":"",
            "User_ShipAddress2":"",
            "User_ShipAddress3":"",
            "User_ShipCity":"",
            "User_ShipState":"",
            "User_ShipCountry":"",
            "User_ShipZip":"",
            "User_BillCompany":"",
            "User_BillFirstName":"",
            "User_BillLastName":"",
            "User_BillAddress1":"",
            "User_BillAddress2":"",
            "User_BillAddress3":"",
            "User_BillCity":"",
            "User_BillState":"",
            "User_BillCountry":"",
            "User_BillZip":"",
            "User_PhoneNo":"",
            "User_FaxNo":"",
            "User_HomeNo":"",
            "User_MobileNo":"",
            "User_OtherNo1":"",
            "User_OtherNo2":"",
            "User_IChat_Aim":"",
            "User_IChat_Gchat":"",
            "User_IChat_MSN":"",
            "User_IChat_Etc":"",
            "User_CreateIPAddress":"",
            "User_CreateDate":"",
            "User_ViewDate":"",
            "User_ModifyDate":"",
            "User_ConvertDate":"",
            "User_LastMailedDate":"",
            "User_LastACSCheckDate":"",
            "User_CCType":"",
            "User_CCNo":"",
            "User_LoggendBy":"",
            "User_ContactVia":"",
            "User_Source":"",
            "User_RelatedAssistant":"",
            "User_Specialties":"",
            "User_JobType":"",
            "User_CompanyType":"",
            "User_TaxID":"",
            "User_Notes":"",
            "User_TermsExtended":"",
            "User_AcsAdvice":"",
            "User_MailList":"",
            "User_EMailList":"",
            "User_PressList":"",
            "User_CustomList":"",
            "User_HolidayList":"",
            "User_VIPList":"",
            "User_MarketingBlacklist":"",
            "User_SalesBlacklist":"",
            "User_IsCustomer":"",
            "User_Answer1":"",
            "User_Answer2":"",
            "User_Answer3":"",
            "User_Answer4":"",
            "User_Answer5":"",
            "User_NewPassword":"",
            "User_OldPassword":"",
            "User_FMUserID":"",
            "User_FMUser":"",
            "User_BetaUser":"",
            "User_ShowFeatures":"",
            "User_TermsVersion":"",
            "User_TOSVersion":"",
            "User_TOSDate":"",
            "User_TOSIP":"",
            "User_TOSLastVersion":"",
            "User_TOSLastDate":"",
            "User_TOSLastIP":"",
            "User_LoginDate":"",
            "User_LoginIPAddress":"",
            "User_FMName":"",
            "User_NameSuffix":"",
            "User_FaxLabel":"",
            "User_OtherNo1Label":"",
            "User_OtherNo2Label":"",
            "User_EmailAddressAlternate":"",
            "User_LoggedBy":"",
            "User_IsObsolete":"",
            "User_AddressSame":"",
            "User_UserDate":"",
            "User_DeferredPay":"",
            "User_TaxExempt":"",
            "User_TaxExemptID":"",
            "User_Saved":"",
            "User_Status":"",
            "User_Migrated":""
        }
    ]
}

2 个答案:

答案 0 :(得分:4)

本地,Backbone.Model需要一个简单的字符串字典:

{
    "name": "Bob",
    "address": "12345 Simple St",
    ...
}

...和Backbone.Collection期望一组简单的字符串字典:

[{
    "name": "Bob",
    "address": "12345 Simple St",
    ...
},{
    "name": "Al",
    "address": "12347 Main St",
    ...
},
...
]

答案 1 :(得分:2)

除了克里斯的回答,我还会增加一些指导。 Backbone是非常灵活的,所以你可以调整它的行为,如果你不遵循这些,但如果你预先计划你的服务,这里有一些其他的东西会让你的生活更轻松:

Backbone希望每个模型(无论是单独返回还是在数组中返回)都具有'id'属性。对于POST请求,不会发送id,但服务器将创建一个'id'并返回它。

对于PUT和POST请求,Backbone希望服务使用与请求正文架构重叠的架构进行响应。换句话说,如果请求包含响应不包含的属性,并且如果响应包含请求没有但没有请求主体的属性包含'name'且响应包含与'ENTITY_NAME'。

避免模式中的嵌套对象。 Backbone有一些扩展,以便在必要时进行此操作,但在可能的情况下更容易避免使用它们。

遵循RESTful URL结构。如果GET / entities返回一个对象数组,每个对象都有一个id属性,那么你应该能够创建一个POST /实体来创建一个新对象,并且GET | PUT | DELETE / entities / someEntityID来检索/修改/删除一个特定实体从列表中。