Falcor语言 - HTTPDataSource to post Json



是否可以使用 falcor.browser 的模型发布 Json 文件?我在其中使用了 get 方法。以下是我需要的,但它不起作用。

<script src="./js/falcor.browser.js"></script>
function registerUser() {
var dataSource = new falcor.HttpDataSource("http://localhost/registerUser.json");
var model = new falcor.Model({
source: dataSource
});
var userJson = {"name":"John","age":"35","email":"john@abc.com"};
model.
set(userJson).
then(function(done){
console.log(done);
});

这是服务器.js代码:

app.use('/registerUser.json', falcorExpress.dataSourceRoute(function (req, res) {
return new Router([
{
route: "rating",
get: function() {
// Post call to external Api goes here
}
}
]);
}));

几件事:

Model 的 set(( 方法采用 1+ 路径值,因此将userJson对象文本重新格式化为一组路径值。 像这样:

model.
set(
{ path: ['users', 'id1', 'name'], value: 'John' },
{ path: ['users', 'id1', 'age'], value: 35 },
{ path: ['users', 'id1', 'email'], value: 'john@abc.com' }
).
then(function(done){
console.log(done);
});

其次,您的路由器必须实现 set 处理程序以对应于您尝试设置的路径。 这些处理程序还应返回 pathValues:

new Router([
{
route: 'users[{keys:ids}]["name", "age", "email"]',
set: function(jsonGraph) {
// jsonGraph looks like { users: { id1: { name: "John", age: 35, email: "john@abc.com" }
// make request to update name/age/email fields and return updated pathValues, e.g.
return [
{ path: ['users', 'id1', 'name'], value: 'John' },
{ path: ['users', 'id1', 'age'], value: 35 },
{ path: ['users', 'id1', 'email'], value: 'john@abc.com' },
];
}
}
]);

鉴于您的数据库请求可能是异步的,您的路由获取处理程序必须返回承诺或可观察。 但以上应该可以作为演示。

编辑

如果字段数量变大,您还可以在第三个路径键上使用路由模式匹配,如上文在第二个 id 键上演示的那样。

{
route: 'users[{keys:ids}][{keys:fields}]',
set: function(jsonGraph) {
/* jsonGraph looks like
{
users: {
id1: { field1: "xxx", field2: "yyy", ... },
id1: { field1: "xxx", field2: "yyy", ... },
...
}
}
*/
}
}

最新更新