将多个Falcor数据源组合成单一模型

时间:2018-03-20 07:28:24

标签: falcor

修改问题以便更好地解释:

我有来自两个不同HttpDataSource的两个Falcor模型,如下所示:

第一个模型(用户模型):

const user_model = new falcor.Model(
{
  source: new HttpDataSource('http://localhost:3000/api/userManagement')
});
user_model.get(['user', 'list'])

OUTPUT1:

{
    "jsonGraph": {
        "user": {
            "list": {
                "$type": "atom",
                "value": {
                    "users": [...]
                }
            }
        }
    }
}

第二个模型(角色模型):

const role_model = new falcor.Model(
{
  source: new HttpDataSource('http://localhost:3000/api/roleManagement')
});

role_model.get(['role', 'list'])

OUTPUT2:

{
    "jsonGraph": {
        "role": {
            "list": {
                "$type": "atom",
                "value": {
                    "roles": [...]
                }
            }
        }
    }
}

有没有办法将所有这些Falcor模型组合成一个模型?

目的是,如果我尝试多次使用user_model.get([' user',' list']),它将从Falcor-Model-Cache获取数据(在第一次从DB获取之后)。

但是如果我尝试做role_model.get([' user',' list']),那么我必须再次点击数据库以获取数据(为了存储在role_model缓存中使用相同的用户列表。

所以相反,如果有这样的方式:

all_model = user_model + role_model

然后我可以做all_model.get(['用户','列出'])(或)all_model.get(['角色',&# 39;列表'])。所以基本上我在浏览器端只有一个Falcor-Model-Cache。

希望现在问题更清楚了。

1 个答案:

答案 0 :(得分:0)

您必须使用forkJoin

forkJoin(model1.source,model2.source).subscribe(res=>{
    //in res[0] you have the response of model1.source
    //in res[1] you have the response of model2.source
    let data={...res[0],...res[1]}
    //in data you have all the properties
}
相关问题