【问题标题】:Falcor - deep nested referencesFalcor - 深层嵌套引用
【发布时间】:2016-01-12 07:48:30
【问题描述】:

我正在和 Falcor 一起玩,看看我工作的公司是否可以使用它,所以我是个新手。

我使用模型作为数据源。

这是我的模型:

var model = new falcor.Model({
  cache:{
    currenciesById: {
      "1": { code: "USD", format: "$" },
      "2": { code: "GBP", format: "£" },
      "3": { code: "EUR", format: "€" },
      "4": { code: "YEN", format: "¥"}
    },
    validCurrencies: {
       "1": { $type: "ref", value: ["currenciesById", 1] },
       "2": { $type: "ref", value: ["currenciesById", 2] },
       "3": { $type: "ref", value: ["currenciesById", 3] },
       "4": { $type: "ref", value: ["currenciesById", 4] }
    },
    quotesByPart: {
      "100" : {price: 1768.34, currency: { $type: "ref", value: ["currencyById", 1] }},
      "200" : {price: 2834.44, currency: { $type: "ref", value: ["currencyById", 2] }},
      "201" : {price: 7803.54, currency: { $type: "ref", value: ["currencyById", 3] }},
      "347" : {price: 389.09, currency: { $type: "ref", value: ["currencyById", 4] }}
    },
    quotes: {
      "1": { $type: "ref", value: ["quotesByPart", 100] },
      "2": { $type: "ref", value: ["quotesByPart", 200] },
      "307": { $type: "ref", value: ["quotesByPart", 347] }
    },
    reservedQuotes:{
      "1": { $type: "ref", value: ["quotesByPart", 201] }
    }
  }
});

当我提出这个要求时:

get('quotes[307].["price", "currency"]') 

这是 Falcor 的回应:

{
   "json": {
      "quotes": {
         "307": {
            "price": 389.09,
            "currency": [
               "currencyById",
               4
            ]
         }
      }
   }
}

这就是我所期待的。 Falcor 发现 quote[307] 实际上是对 quotesByPart[347] 的引用并解析它,实际上它返回了正确的价格和对货币的引用。

然后我尝试让 Falcor 在同一个请求中解析第二个参考 - 货币。 我试着写这样的请求:

get('quotes[307].["currency"].["code"]')

或者,出于绝望

get('quotes[307].["currency.code"]') 

我无法让 Falcor 解析第二个引用。

有人能告诉我我在这里缺少什么吗?

【问题讨论】:

    标签: javascript node.js netflix falcor


    【解决方案1】:

    问题出在我的模型上。

    正确的模型是下面这个:

    var model = new falcor.Model({
      cache:{
        currencyById: {
          "1": { code: "USD", format: "$" },
          "2": { code: "GBP", format: "£" },
          "3": { code: "EUR", format: "€" },
          "4": { code: "YEN", format: "¥"}
        },
        validCurrencies: {
            "1": { $type: "ref", value: ["currencyById", 1] },
            "2": { $type: "ref", value: ["currencyById", 2] },
            "3": { $type: "ref", value: ["currencyById", 3] },
            "4": { $type: "ref", value: ["currencyById", 4] }
        },
        quotesByPart: {
          "100" : {price: 1768.34, currency: { $type: "ref", value: ["currencyById", 1] }},
          "200" : {price: 2834.44, currency: { $type: "ref", value: ["currencyById", 2] }},
          "201" : {price: 7803.54, currency: { $type: "ref", value: ["currencyById", 3] }},
          "347" : {price: 389.09, currency: { $type: "ref", value: ["currencyById", 4] }}
        },
        quotes: {
          "1": { $type: "ref", value: ["quotesByPart", 100] },
          "2": { $type: "ref", value: ["quotesByPart", 200] },
          "307": { $type: "ref", value: ["quotesByPart", 347] }
        },
        reservedQuotes:{
          "1": { $type: "ref", value: ["quotesByPart", 201] }
        }
      }
    });
    

    使用此模型,Falcor 可以按预期工作,甚至可以解析深层嵌套引用。

    【讨论】:

    • 问题是currenciesById 应该是currencyById?好的,获取引用的price 以及currencycodeformat 的语法是什么?是get('quotes[307].["price", "currency.code","currency.format"]')吗? Falcor Paths 的文档仍然缺乏......
    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 2020-12-10
    • 2014-06-19
    • 2021-12-20
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多