2015-10-18 30 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); 

這是有點快扔在一起,所以我道歉,如果它不工作,直客,有任何問題,我會幫你解決。

相關問題