【问题标题】:How to structure falcor router to get all available IDs?如何构建 falcor 路由器以获取所有可用的 ID?
【发布时间】:2015-11-13 01:55:53
【问题描述】:

我正在尝试使用 Falcor 放在 Guild Wars 2 API 前面,并想用它来显示游戏物品的详细信息。我对构建一个可以使用多个数据源来组合不同 API 的结果的路由器特别感兴趣。

关键是,激战 2 中的Item IDs 不是连续的。这是一个例子:

[
    1,
    2,
    6,
    11,
    24,
    56,
    ...
]

所以我不能像items[100..120].name 这样在客户端上写路径,因为几乎可以肯定该列表中会有很多漏洞。

我尝试向我的路由器添加一个路由,这样我就可以请求items,但这会将它发送到客户端上的无限循环中。你可以看到那个尝试on GitHub

关于构建这个的正确方法的任何指针?当我想得更多时,也许我想要item.id

【问题讨论】:

    标签: falcor falcor-router


    【解决方案1】:

    您可以使用 Falcor 的 'get' API,它检索多个值。您可以传递任意数量的必需属性,如下所示

    var model=new falcor.Model({
                 cache:{       
                         genereList:[
                                        {name:"Recently Watched",
                                         titles:[
                                             {id:123,
                                              name: "Ignatius",
                                             rating: 4}
                                         ]    
                                        },
                                        {name:"New Release",
                                         titles:[
                                             {id:124,
                                              name: "Jessy",
                                             rating: 3}
                                         ]    
                                        }
                                    ]
                                }
    
                });
                Getting single value
                model.getValue('genereList[0].titles[0].name').
                then(function(value){
                   console.log(value);
               });
    
                Getting multiple values
                model.get('genereList[0..1].titles[0].name', 'genereList[0..1].titles[0].rating').
                then(function(json){
                    console.log(JSON.stringify(json, null, 4));
                })
    

    【讨论】:

      【解决方案2】:

      您不应该发现自己从 Falcor JSON Graph 对象中询问 id。

      您似乎想要构建一个游戏 id 数组:

      {
          games: [
              { $type: "ref", value: ["gamesById", 352] },
              { $type: "ref", value: ["gamesById", 428] }
              // ...
          ],
          gamesById: {
              352: {
                  gameProp1: ...,
              },
              428: {
                  gameProp2: ...
              }
          }
      }
      
      [games, {from: 5, to: 17 }, "gameProp1"]
      

      这行得通吗?

      【讨论】:

      • 也许我只是误解了如何使用 Falcor/JSON Graph,我可能是。我想显示前 100 件商品及其名称/图标/价格。向我的路由器询问items[1..100]['id', 'name', 'price'] 似乎很简单,但我正在思考如何构建服务器端组件以使其正确返回。
      • 需要更仔细地查看,items.lengthitems 的组合将引用返回到 itemsById getter 是诀窍。 github.com/tivac/falcor-experiment/commit/…
      猜你喜欢
      • 2015-11-18
      • 2016-07-25
      • 2016-07-07
      • 2015-07-04
      • 2021-10-14
      • 2016-02-12
      • 2015-12-08
      • 2015-12-12
      • 2016-05-17
      相关资源
      最近更新 更多