2015-10-18 54 views
0

我正在使用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) 
    }]); 

我试图遍历“资源”,这是在课程对象嵌套数组中,“ResourceList”集合中查找每个值,并在返回新数组范围。

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

我的想法是异步调用正在进行,但一直没能找到解决方案。我仍然安装了autopublish软件包。任何帮助,将不胜感激。

+1

查看[发布复合材料](https://atmospherejs.com/reywood/publish-composite)的气氛,你将能够获得这一切完成服务器端并节省了很多问题。如果你想要一个演示,我会把它写成答案。你在这里遇到的问题是角度和页面正确刷新控制器的问题。 –

+0

生病看看我可以采取这一点,感谢指针。 – GMarsh

+0

@TjGienger。感谢您的建议。看起来这可以做我所需要的,但我不知道如何完成它。在示例中,他们使用find返回一个游标(例如,具有多个帖子)。然后他们重复这些并做他们想要的。相反,我期待找到一个,然后遍历该文档中的嵌套字段,将每个文档映射到另一个集合中的文档。有没有办法做到这一点与发布组合包?演示将是惊人的。谢谢。 – GMarsh

回答

0

你要做的是返回一个包含你想要的所有信息的游标,然后你可以在客户端使用$ meteor.object(如果你喜欢的话)。通常情况下,publishComposite会是这个样子:(我不知道你的curriculum.resources的样子)

使用此方法如果curriculum.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; 
}); 

那么应该发生在这里(我没有测试)是一个发布功能,你会得到你想要的课程,再加上它的所有resourceslist孩子。

现在你将有机会在客户端访问这些。

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

这是有点快扔在一起,所以我道歉,如果它不工作,直客,有任何问题,我会帮你解决。