简体   繁体   English

Falcor-HTTPDataSource发布Json

[英]Falcor - HTTPDataSource to post Json

Is it possible to post a Json file using the falcor.browser's model? 是否可以使用falcor.browser的模型发布Json文件? I have used the get method in it. 我已经在其中使用了get方法。 Below is what I require, but it is not working. 以下是我所需要的,但是它不起作用。

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

This is the server.js code: 这是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
      }
    }
  ]);
}));

A few things: 一些东西:

The Model's set() method takes 1+ pathValues , so reformat your userJson object literal into a set of pathValues. Model的set()方法采用1+ pathValues ,因此将userJson对象文字重新格式化为一组pathValues。 Something like: 就像是:

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

Second, your router must implement set handlers to correspond to the paths you are trying to set. 其次,路由器必须实现设置处理程序以对应于您要设置的路径。 These handlers should also return pathValues: 这些处理程序还应该返回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' },
          ];
      }
    }
  ]);

Given that your DB request is likely asynchronous, your route get handler will have to return a promise or observable. 鉴于您的数据库请求可能是异步的,因此您的路由获取处理程序将必须返回一个Promise或Observable。 But the above should work as a demonstration. 但是以上内容应作为示例。

Edit 编辑

You can also use route pattern matching on the third path key if the number of fields gets large, as was demonstrated above on the second id key. 如果字段数量变大,您也可以在第三个路径键上使用路由模式匹配 ,如上面在第二个id键上所示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM