【问题标题】:Looping through array of object data within Falcor循环遍历 Falcor 中的对象数据数组
【发布时间】:2016-01-02 10:14:45
【问题描述】:

假设我有以下路线:

{
  route: "usersById['length']",
  get: function(pathSet) {}
},
{
  route: "usersById[{integers:ids}]['firstName', 'lastName']",
  get: function(pathSet) {}
}

在我的 angular1 控制器中使用以下内容:

Model.get(
  'usersById.length',
  'usersById[0..2]['firstName', 'lastName']'
).then(function(response) {
  $scope.$apply(function() {
    vm.entities = response.json.usersById;
  });
});

来自服务器的响应将类似于:

{
  jsonGraph: {
    usersById: {
      "0": {
        firstName: 'Jiminy',
        lastName: 'Cricket'
      },
      "1": {
        firstName: 'Jafar',
        lastName: 'Husain'
      },
      "length": 123123
    }
  }
}

在我的 angular 1 模板中,我想遍历用户列表:

<tr ng-repeat="entity in users.entities">
  <td>{{entity.firstName}} {{entity.lastName}}</td>
</tr>

问题在于响应中不仅有用户,首先它包含length,其次似乎模型的承诺返回了其他元数据,其中看起来是路径数据的一部分:usersById

循环浏览用户列表的首选方式是什么?我应该在我的承诺中做这样的事情吗?

vm.entities = response.json.usersById.filter(function(value) {
  return typeof value === 'object';
});

我没有看到任何用于在任何地方获取原始值的 API 调用。

【问题讨论】:

    标签: angularjs falcor falcor-router


    【解决方案1】:

    好的,因此处理此问题的正确方法似乎是创建另一条路线:users 仅返回 usersById 引用,这样您就拥有一个仅包含实体的数组,不包含长度等。

    我猜数据数组中的路径数据只是一个错误。

    {
        users: {...},
        usersById: {...}
    }
    
    <li ng-repeat="user in users">{{ user.firstName }}</li>
    

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      • 2017-01-29
      • 2015-10-15
      • 2015-12-28
      相关资源
      最近更新 更多