【问题标题】:Falcor route specific middlewareFalcor 路由特定中间件
【发布时间】:2015-11-13 08:32:55
【问题描述】:

给定在服务器上运行的以下路由器类:

var PetsRouterBase = Router.createClass([{
  route: 'petList[{integers:indices}].name',
  get: function(pathSet) {

    return [
      { 
        path: ['petList', 0, 'name'], 
        value: 'Pets I have now'
      },
      { 
        path: ['petList', 1, 'name'], 
        value: 'Pets I once had'
      },
      { 
        path: ['petList', 2, 'name'], 
        value: 'Pets my friends have'
      }
    ];
  }
}]);

以及浏览器中的如下路径查询(我使用的是falcor-http-datasource):

model.get('petList[0..2].name');

我得到了正确的数据:

{
  "jsonGraph": {
    "petList": { 
      "0":{"name":"Shows of Artists I've been to before",
      "1":{"name":"Shows of artists my friends have been to before",
      "2":{"name":"Highly rated artists"}
    }
  }
}

我的问题是,在服务器上,我是否有一种方法可以访问 falcor 响应此获取路由请求通过线路发送回浏览器的实际结果?

我的用例是我想同时注销两条数据:

  1. 路由通过的 pathSet。
  2. falcor 通过网络发回的 json 结果。

我在想它可能看起来像这样:

var PetsRouterBase = Router.createClass([{
  route: 'petList[{integers:indices}].name',
  done: function(pathSet, results) {
    // Log out the result of the lookup
    console.log(pathSet, results); 
  },
  get: function(pathSet) {

    return [
      { 
        path: ['petList', 0, 'name'], 
        value: 'Pets I have now'
      },
      { 
        path: ['petList', 1, 'name'], 
        value: 'Pets I once had'
      },
      { 
        path: ['petList', 2, 'name'], 
        value: 'Pets my friends have'
      }
    ];
  }
}]);

只是为了清楚。我知道我可以在客户端获得结果,但我想将它们通过管道传输到服务器上的其他地方。

【问题讨论】:

    标签: javascript node.js falcor falcor-router


    【解决方案1】:

    目前最简单的事情是在将路由器发送到快速中间件之前装饰路由器。

    app.use('/model.json', FalcorServer.dataSourceRoute(function(req, res) {
        return {
            get: function(pathSets) {
                // print incoming paths to console
                console.log(JSON.stringify(pathSets, null, 4));
                return router.
                    get(pathSets).
                    // print the results to the console
                        doAction(function(output) {
                            console.log(JSON.stringify(output, null, 4));    
                        });
            }
        };
    })
    

    【讨论】:

    • 完美!我使用了这种方法,并且效果很好。我确实必须为他们的方法签名添加 set 和 call with wrappers。
    猜你喜欢
    • 2016-07-25
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 2016-02-12
    相关资源
    最近更新 更多