假设服务器上运行以下 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 通过线路发送回浏览器以响应此获取路由请求的实际结果?
我的用例是我想一起注销两条数据:
- 路由经过的pathSet。
- 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/