3

Working on a proprietary CRM application using Backbone / Marionette and I'm wondering, what structure does Backbone expect for its JSON? This is what I'm currently working with, but I can change it to whatever will work best. (I'd like to avoid having to set up a custom parse function if possible.)

{
    "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":""
        }
    ]
}
4

2 回答 2

4

Natively, Backbone.Model expects a simple string dictionary:

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

...and Backbone.Collection expects an array of simple string dictionaries:

[{
    "name": "Bob",
    "address": "12345 Simple St",
    ...
},{
    "name": "Al",
    "address": "12347 Main St",
    ...
},
...
]
于 2013-09-08T17:14:26.210 回答
2

In addition to Chris's answer I would add some more guidelines. Backbone is very flexible so you can tweak it's behavior if you don't follow these but here are some other things that will make your life easier if you plan your services around them up front:

Backbone would like each model (whether returned alone or in an array) to have an 'id' attribute. For POST requests, the id would not be sent but the server will create an 'id' and return it.

For PUT and POST requests, Backbone would like the service to respond with a schema that overlaps with the schema of the request body. In other words, its ok if the request includes attributes that the response wont include and its ok if the response includes attributes that the request didn't but don't have the request body include 'name' and the response include this same value as 'entity_name'.

Avoid nested objects in your schemas. There are some extensions to Backbone to facilitate this where necessary but it's easier to avoid them when possible.

Follow a RESTful URL structure. If GET /entities returns an array of objects which each have an id attribute, then you should be able to do POST /entities to create a new object and GET|PUT|DELETE /entities/someEntityID to retrieve/modify/delete a specific entity from the list.

于 2013-09-09T00:39:59.223 回答