是否可以使用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);
});这是server.js代码:
app.use('/registerUser.json', falcorExpress.dataSourceRoute(function (req, res) {
return new Router([
{
route: "rating",
get: function() {
// Post call to external Api goes here
}
}
]);
}));发布于 2017-08-26 01:35:23
以下是一些事情:
模型的set()方法接受1+ pathValues,因此将userJson对象文本重新格式化为一组pathValues。类似于:
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 handlers,以便与您尝试设置的路径相对应。这些处理程序还应返回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' },
];
}
}
]);由于您的DB请求可能是异步的,因此您的路由get处理程序必须返回一个promise或observable。但以上内容应该可以作为一个示例。
编辑
如果字段的数量变大,还可以在第三个路径键上使用route pattern matching,正如上面在第二个id键上所演示的那样。
{
route: 'users[{keys:ids}][{keys:fields}]',
set: function(jsonGraph) {
/* jsonGraph looks like
{
users: {
id1: { field1: "xxx", field2: "yyy", ... },
id1: { field1: "xxx", field2: "yyy", ... },
...
}
}
*/
}
}https://stackoverflow.com/questions/45837640
复制相似问题