【问题标题】:Angular Meteor objects not acting as expectedAngular Meteor 对象未按预期运行
【发布时间】:2015-10-24 00:30:33
【问题描述】:

我正在使用 Angular Meteor,但我的对象/数组有问题。我有这个代码:

angular.module("learn").controller("CurriculumDetailController", ['$scope', '$stateParams', '$meteor',
  function($scope, $stateParams, $meteor){
    $scope.curriculum = $meteor.object(CurriculumList, $stateParams.curriculumId);

    $scope.resources = _.map($scope.curriculum.resources, function(obj) {
      return ResourceList.findOne({_id:obj._id})
    });

    console.log($scope.resources)
  }]);

我正在尝试遍历 'resources',它是课程对象中的一个嵌套数组,查找 'ResourceList' 集合中的每个值,并返回范围内的新数组。

问题是,有时有效,有时无效。当我加载页面并通过 UI 路由器链接访问它时。我得到了预期的数组。但是如果页面被刷新,$scope.resources 是一个空数组。

我的想法是异步调用发生了一些事情,但无法找到解决方案。我仍然安装了自动发布包。任何帮助将不胜感激。

【问题讨论】:

  • 在气氛中查看publish composites,您将能够在服务器端完成这一切并节省很多问题。如果你想要一个演示,我会把它写下来作为答案。您在这里遇到的问题是角度和页面正确刷新控制器的问题。
  • 我看看哪里可以拿这个,谢谢指点。
  • @TjGienger。谢谢你的建议。看起来这可以做我需要的,但我不知道如何完成它。在示例中,他们使用 find 返回一个游标(例如,有多个帖子)。然后他们遍历这些并做他们想做的事。相比之下,我正在寻找 findOne 然后遍历该文档中的嵌套字段,将每个字段映射到另一个集合中的文档。有没有办法用发布复合包做到这一点?演示会很棒。谢谢。

标签: angular-meteor


【解决方案1】:

你要做的是返回一个包含你想要的所有信息的游标,然后你可以在客户端使用 $meteor.object 如果你愿意。通常,publishComposite 看起来像这样:(我不知道您的课程表。资源是什么样的)

如果课程表.resources 只有一个 id,请使用此方法:

// this takes the place of the publish method
Meteor.publishComposite('curriculum', function(id) {
  return {
      find: function() {

        // Here you are getting the CurriculumList based on the id, or whatever you want
        return CurriculumList.find({_id: id});
      },
      children: [
        {
          find: function(curr) {
            // (curr) will be each of the CurriculumList's found from the parent query
            // Normally you would do something like this:
            return ResourceList.find(_id: curr.resources[0]._id);
          }
        }
      ]
    }
})

如果你有多个资源,这个方法:

但是,由于您的课程看起来将有一个资源列表,其中包含一个或多个具有 id 的对象,因此我们需要在返回任何内容之前构建查询。尝试类似:

// well use a function so we can send in an _id
Meteor.publishComposite('curriculum', function(id){
    // we'll build our query before returning it.
    var query = {
        find: function() {
            return CurriculumList.find({_id: id});
        }
    };        

    // now we'll fetch the curriculum so we can access the resources list
    var curr = CurriculumList.find({_id: id}).fetch();

    // this will pluck the ids from the resources and place them into an array
    var rList = _.pluck(curr.resources, '_id');

    // here we'll iterate over the resource ids and place a "find" object into the query.children array.

    query.children = [];

    _.each(rList, function(id) {
        var childObj = {
            find: function() {
                return ResourceList.find({_id: id});
            }
        };
        query.children.push(childObj)
    })

    return query;
});

所以应该在这里发生的事情(我没有测试)是使用一个发布功能,您将获得您想要的课程,以及所有它的资源列表子项。

现在您可以在客户端访问这些。

$scope.curriculum = $meteor.object(CurriculumList, $stateParams.curriculumId);
// collection if more than one, object if only one.
$scope.resources = $meteor.collection(ResoursesList, false);

这有点快,所以如果它不能立即工作,我很抱歉,我会帮你解决任何问题。

【讨论】:

    猜你喜欢
    • 2015-06-08
    • 2018-10-21
    • 2021-10-29
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 2019-03-09
    相关资源
    最近更新 更多