简体   繁体   English

Backbone.js JSON结构

[英]Backbone.js JSON Structure

I am confused on how to work with the JSON structure of an API with backbone. 我对如何使用带有主干的API的JSON结构感到困惑。

I have no control over the structure of the api (it is the REST api from the wordpress plugin - woocommerce). 无法控制 api 的结构 (它是wordpress插件woocommerce的REST api)。

I have relatively little experience with Backbone and every json api, which I have always created myself has had the following structure: 我对Backbone和每个json API的经验都很少,我一直创建自己的api具有以下结构:

[
    {
        "id": "1",
        "name": "John",
        "email": "john@example.com",
        "order_number": "#3473388"
    },
    {
        "id": "2",
        "name": "Jane Doe",
        "email": "jane@example.com",
        "order_number": "#8877632"
    }
]

An array of objects, nice and simple! 一系列对象,美观又简单!

However, the structure of the api I am working with now has the following stucture: 但是,我现在使用的api的结构具有以下结构:

{
    "orders": [
        {
            "id": 130259,
            "name": "John Doe",
            "email": "john@example.com",
            "order_number": "#3473388"
        },
        {
            "id": 130259,
            "name": "Jane Doe",
            "email": "jane@example.com",
            "order_number": "#3473388"
        }
    ]
}

This is my model for an order: 这是我的订单模型:

App.Models.Order = Backbone.Model.extend({

    defaults: {
        id: '',
        name: '',
        email: '',
        order_number: ''
    }

});

And this is my collection of orders: 这是我的订单集合:

App.Collections.Orders = Backbone.Collection.extend({

    model: App.Models.Order,

    url: 'http://api.example.com/orders'

});

The structure of my model/collection does not map well with the api, and when fetching my collection, the data is returned, but doesn't fit. 我的模型/集合的结构无法与api很好地映射,并且在fetching我的集合时,会返回数据,但不合适。 How do I work with backbone to accomadate this json structure? 我如何与骨干网配合这个json结构?

UPDATE UPDATE

Overriding the parse method on the collection (thanks to Yura) like so returns the data: 像这样重写集合上的parse方法(感谢Yura),将返回数据:

App.Collections.Orders = Backbone.Collection.extend({

    model: App.Models.Order,

    url: 'http://api.example.com/orders',

    parse: function( resp ){
        return resp.orders;
    }

});

The data returned is multidimensional like so: 返回的数据是多维的,如下所示:

[
    {
        "id": 130285,
        "order_number": "#130285",
        "billing_address": {
            "first_name": "John",
            "last_name": "Doe",
            "company": "",
            "address_1": "13 Green Willow",
            "address_2": "",
            "city": "Celbridge",
            "state": "Kildare",
            "postcode": "",
            "country": "IE",
            "email": "johndoe@hotmail.com",
            "phone": "87384348043"
        }
    }
]

In the parse method, how do I map multidimensional json to my collection's models? 在解析方法中,如何将多维json映射到集合的模型?

Since your API returns an array of objects that can be used to initialize models, you can use parse on your collection, like so: 由于您的API返回了可用于初始化模型的对象数组,因此可以对集合使用parse ,如下所示:

App.Collections.Orders = Backbone.Collection.extend({

    model: App.Models.Order,

    url: 'http://api.example.com/orders',

    parse: function( resp ){
        return resp.orders;
    }

});

If your API returned plain array of objects, you wouldn't need parse . 如果您的API返回对象的纯数组,则不需要parse
Documentation for Collection.parse: http://backbonejs.org/#Collection-parse Collection.parse的文档: http : //backbonejs.org/#Collection-parse

Update 更新

If you need to parse individual objects to model keys, you can add parse method on your model and return the object where keys will be the keys of your models and values passed from your response. 如果需要解析单个对象以建模键,则可以在模型上添加parse方法,然后返回该对象,其中键将是模型的键和从响应传递的值。

I have created a jsfiddle for demonstration: http://jsfiddle.net/yuraji/x62jje39/ 我创建了一个jsfiddle进行演示: http : //jsfiddle.net/yuraji/x62jje39/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM