javascript - Falcor 路由特定中间件

标签 javascript node.js falcor falcor-router

假设服务器上运行以下 Router 类:

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'
      }
    ];
  }
}]);

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

最佳答案

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

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));    
                    });
        }
    };
})

关于javascript - Falcor 路由特定中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32105719/

相关文章:

javascript - 使用 Chart.js 将日期作为 x 轴标签

python - Python 的 pbkdf2_sha256.verify 的 NodeJS 实现

node.js - 多个字段的 Sequelize 分组会导致 Sequelize 错误

falcor - 我需要解释 Falcor 的 `call` 方法;或者一个很好的例子?

javascript - jQuery 用动画改变图像并等待它触发超链接

javascript - 在其外部单击时,多个模态框不会关闭

javascript - 从这个 google places API 地址结果中提取地址片段的最佳方法是什么?

node.js - 配置 Passport 接受没有正文的请求?

javascript - Falcor - 深层嵌套引用