【问题标题】:What is the expected structure for JSON in a RESTful Backbone application?RESTful Backbone 应用程序中 JSON 的预期结构是什么?
【发布时间】:2013-09-08 16:33:49
【问题描述】:

使用 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":""
        }
    ]
}

【问题讨论】:

  • “格式化”是什么意思?
  • ideal 是什么意思,有效的 JSON 是理想的。
  • @Pointy - 只是 JSON 的格式。
  • 您使用“格式”一词来解释“格式”的含义。 JSON 解析器完全忽略标记之间的空白。
  • @limelights — 好吧,在我上面的示例中,最好将 records 设为根元素,像这样吗? hastebin.com/jalaxonuja.ini

标签: javascript json backbone.js marionette


【解决方案1】:

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

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

...Backbone.Collection 需要一个简单的字符串字典数组:

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

【讨论】:

  • 正是我一直在寻找的,并且为我清理了很多东西。谢谢!!
【解决方案2】:

除了 Chris 的回答之外,我还要添加一些准则。 Backbone 非常灵活,因此如果您不遵循这些规则,您可以调整其行为,但如果您围绕它们预先规划服务,这里还有一些其他的东西可以让您的生活更轻松:

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

对于 PUT 和 POST 请求,Backbone 希望服务以与请求正文的架构重叠的架构进行响应。换句话说,如果请求包含响应不会包含的属性,则可以;如果响应包含请求不包含但请求正文不包含“名称”的属性,并且响应包含与此相同的值,则可以“实体名称”。

避免架构中的嵌套对象。 Backbone 有一些扩展可以在必要时促进这一点,但尽可能避免使用它们更容易。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-10
    • 2023-04-11
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多