Javascript获取请求

时间:2018-03-08 16:02:31

标签: javascript fetch flux

我目前有两个数据文件。第一个是我所关注的项目清单,即

PROJECT_DATA_LIST

{"id": "1a", "name": "project1"},
{"id": "2b", "name": "project2"}

第二个是更具体的每个项目的数据列表:

PROJECT_DATA

var project1 = [
{
    group: "Alpha",
    data: [
        {
            label: "Accounting",
            data: [
                {timeRange: ["2018-02-26 11:40:00 AM", "2018-02-26 11:40:20 AM"], val: "TargetA"},
                {timeRange: ["2018-02-26 11:40:20 AM", "2018-02-26 11:40:40 AM"], val: "TargetA"},
                {timeRange: ["2018-02-26 11:41:00 AM", "2018-02-26 11:41:20 AM"], val: "TargetA"},
            ]
        },
        {
            label: "Engineering",
            data: [
                {timeRange: ["2018-02-26 11:40:40 AM", "2018-02-26 11:41:00 AM"], val: "TargetB"},
                {timeRange: ["2018-02-26 11:41:00 AM", "2018-02-26 11:41:20 AM"], val: "TargetB"}
            ]
        },
        {
            label: "Marketing",
            data: [
                {timeRange: ["2018-02-26 11:40:40 AM", "2018-02-26 11:41:00 AM"], val: "TargetC"},
                {timeRange: ["2018-02-26 11:41:00 AM", "2018-02-26 11:41:20 AM"], val: "TargetC"},
            ]
        },
    ]
},
{
    group: "Beta",
    data: [
        {
            label: "Accounting",
            data: [
                {timeRange: ["2018-02-26 11:40:20 AM", "2018-02-26 11:40:40 AM"], val: "TargetA"},
                {timeRange: ["2018-02-26 11:41:00 AM", "2018-02-26 11:41:20 AM"], val: "TargetA"},
            ]
        },
        {
            label: "Engineering",
            data: [
                {timeRange: ["2018-02-26 11:40:40 AM", "2018-02-26 11:41:00 AM"], val: "TargetB"},
                {timeRange: ["2018-02-26 11:41:00 AM", "2018-02-26 11:41:20 AM"], val: "TargetB"},
                {timeRange: ["2018-02-26 11:41:20 AM", "2018-02-26 11:41:40 AM"], val: "TargetB"},
                {timeRange: ["2018-02-26 11:41:40 AM", "2018-02-26 11:42:00 AM"], val: "TargetB"},
                {timeRange: ["2018-02-26 11:42:00 AM", "2018-02-26 11:42:20 AM"], val: "TargetB"}
            ]
        },
        {
            label: "Marketing",
            data: [
                {timeRange: ["2018-02-26 11:40:40 AM", "2018-02-26 11:41:00 AM"], val: "TargetC"},
                {timeRange: ["2018-02-26 11:41:00 AM", "2018-02-26 11:41:20 AM"], val: "TargetC"},
            ]
        }
    ]
}
];
var project2 = [
    {
        group: "Alpha",
        data: [
            {
                label: "Accounting",
                data: [
                    {timeRange: ["2018-02-26 11:40:00 AM", "2018-02-26 11:40:20 AM"], val: "TargetA"},
                    {timeRange: ["2018-02-26 11:40:20 AM", "2018-02-26 11:40:40 AM"], val: "TargetA"},
                    {timeRange: ["2018-02-26 11:41:00 AM", "2018-02-26 11:41:20 AM"], val: "TargetA"},
                ]
            },
            {
                label: "Engineering",
                data: [
                    {timeRange: ["2018-02-26 11:40:40 AM", "2018-02-26 11:41:00 AM"], val: "TargetB"},
                    {timeRange: ["2018-02-26 11:41:00 AM", "2018-02-26 11:41:20 AM"], val: "TargetB"}
                ]
            },
            {
                label: "Marketing",
                data: [
                    {timeRange: ["2018-02-26 11:40:40 AM", "2018-02-26 11:41:00 AM"], val: "TargetC"},
                    {timeRange: ["2018-02-26 11:41:00 AM", "2018-02-26 11:41:20 AM"], val: "TargetC"},
                ]
            },
        ]
    },
    {
        group: "Beta",
        data: [
            {
                label: "Accounting",
                data: [
                    {timeRange: ["2018-02-26 11:40:20 AM", "2018-02-26 11:40:40 AM"], val: "TargetA"},
                    {timeRange: ["2018-02-26 11:41:00 AM", "2018-02-26 11:41:20 AM"], val: "TargetA"},
                ]
            },
            {
                label: "Engineering",
                data: [
                    {timeRange: ["2018-02-26 11:40:40 AM", "2018-02-26 11:41:00 AM"], val: "TargetB"},
                    {timeRange: ["2018-02-26 11:41:00 AM", "2018-02-26 11:41:20 AM"], val: "TargetB"},
                    {timeRange: ["2018-02-26 11:41:20 AM", "2018-02-26 11:41:40 AM"], val: "TargetB"},
                    {timeRange: ["2018-02-26 11:41:40 AM", "2018-02-26 11:42:00 AM"], val: "TargetB"},
                    {timeRange: ["2018-02-26 11:42:00 AM", "2018-02-26 11:42:20 AM"], val: "TargetB"}
                ]
            },
            {
                label: "Marketing",
                data: [
                    {timeRange: ["2018-02-26 11:40:40 AM", "2018-02-26 11:41:00 AM"], val: "TargetC"},
                    {timeRange: ["2018-02-26 11:41:00 AM", "2018-02-26 11:41:20 AM"], val: "TargetC"},
                ]
            }
        ]
    }
];

我正在尝试使用第一个文件作为获取特定项目数据的密钥。因此,对于第一个文件中的“project1”,从第二个文件返回 project1 数据。我知道我需要使用javascript fetch /然后完成此操作。类似的东西:

fetch(project_list)
    .then( function (project_data) {

//fetch the data for each project
    for(var project of project_data){

        //fetch data for specific project
        fetch(project_data_list).then(function( project_data ){

            //add project data to run data store
            store.add(...)
        })

    }

})

但是,我的意思是,这显然是错误的(并且不完整)。有关如何实现这一目标的任何想法?

我正在使用FLUX设计模式,因此使用store.add。

0 个答案:

没有答案