zoukankan      html  css  js  c++  java
  • [Falcor] Indroduce to Model

    How to work with JSON data indirectly through a Falcor Model. The Falcor Model allows you to work with data using the same familiar JavaScript path syntax. However the Model uses a push API, sending the data to a callback rather than returning it immediately. Using a push API means that you can move your data anywhere in the network later on, without changing the data retrieval code in your client.

    Below the code, show the data stay in memory, and we get the data by javascript path:

    <!-- index.html -->
    <html>
    <head>
        <!-- Do _not_  rely on this URL in production. Use only during development.  -->
        <script src="//netflix.github.io/falcor/build/falcor.browser.js"></script>
        <script>
            var model = {
              people: [
                  {
                      name: "Zhentian",
                      titles: [
                          {
                              id: 123,
                              name: "developer",
                              rating: 10
                          }
                      ]
                  },
                  {
                      name: "Otto",
                      titles: [
                          {
                              id: 321,
                              name: "developer",
                              rating: 10
                          }
                      ]
                  }
              ]  
            };
            
            console.log(model.people[0].titles[0].name);
        </script>
    </head>
    <body>
    </body>
    </html>

    You will see the "developer" in the console.

    Using farcol.Model to the data async, the data will be pushed into the console:

    <!-- index.html -->
    <html>
    <head>
        <!-- Do _not_  rely on this URL in production. Use only during development.  -->
        <script src="//netflix.github.io/falcor/build/falcor.browser.js"></script>
        <script>
            var model = new falcor.Model({
                cache: {
                    people: [
                        {
                            name: "Zhentian",
                            titles: [
                                {
                                    id: 123,
                                    name: "developer",
                                    rating: 10
                                }
                            ]
                        },
                        {
                            name: "Otto",
                            titles: [
                                {
                                    id: 321,
                                    name: "developer",
                                    rating: 10
                                }
                            ]
                        }
                    ]
                }
            });
            
            model.getValue('people[0].titles[0].name')
                    .then(function (value){
                        console.log("Falcor: ", value);
                    });
        </script>
    </head>
    <body>
    </body>
    </html>

    We still use the javascript path the repersent the data:

    'people[0].titles[0].name'

    getValue return a promise, so we use .then to get the value:

            model.getValue('people[0].titles[0].name')
                    .then(function (value){
                        console.log("Falcor: ", value);
                    });
  • 相关阅读:
    由ping百度引发的思考
    操作系统 | 概述
    操作系统导论第四章 | 抽象:进程
    汇编语言 | 定制键盘输入的处理过程
    细数 TS 中那些奇怪的符号
    vue 各种 import 引入
    display:table-cell实现水平垂直居中
    Jquery判断单选框是否选中和获取选中的值
    css整理 -- 右箭头,上下箭头,三角形、超出省略号代替
    JQuery操作select下拉框
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4948883.html
Copyright © 2011-2022 走看看