2015-11-12 212 views
0

設的列表呈遞每個孩子的信息說,有是在不同schools類(groups)的students一些數據:在非規範化的數據結構

{ 
    "students": { 
    "alexa" : { 
     "firstName" : "ReJina", 
     "grade" : 2, 
     "lastName" : "idk", 
     ... 
    }, 
    "bobby" : { ... }, 
    "chris" : { ... }, 
    ... 
    }, 
    "schools": { 
    "springfield" : { 
     "name" : "Springfield Elementary", 
     "location" : "123 Main St", 
     ... 
    }, 
    "quahog" : { ... }, 
    "zion" : { ... }, 
    ... 
    }, 
    "group": { 
    "one" : { 
     "members" : { 
     "alexa" : true, 
     "bobby" : true, 
     "chris" : true 
     }, 
     "school" : { 
     "springfield" : true 
     }, 
     "name" : "Math Class" 
    }, 
    "two" : { 
     "members" : { 
     "bart" : true, 
     "lisa" : true, 
     "meg" : true, 
     ... 
     }, 
     "school" : { 
     "quahog" : true 
     }, 
     "name" : "Art Class" 
    }, 
    "three" : { ... }, 
    "four" : { ... }, 
    ... 
    } 
} 

據我所知,這是正確的方式以平坦的方式構建數據。請讓我知道如果這是不正確的。我保留每個項目的相關名單,所以每個班級都需要一個名冊(members),如果我也有學生在一個以上班級(group)或學校的情況,那麼他們需要他們參加的班級和學校名單。

現在我們需要顯示所有的學校類(groups),學生在每個班級和學生在同一視圖與嵌套ng-repeat視圖中的HTML

<div id="data-table-div" class="col-md-6"> 
    // Get all the groups, and show them one by one 
    <div ng-repeat="group in groups"> 
    // This works - How do I get the school it is associated to? 
    //    ?? 
    {{group.name}} - {{group's school's name}} 

    //Start a table 
    <table> 
     <thead> <tr> <th>Student Name</th> <th>Grade</th> ... </thead> 
     <tbody> 
     // Get the students in each group, and show them one by one 
     //      ?? 
     <tr ng-repeat="student in groups.members"> 
      <td>{{student.name}}</td> <td>{{student.grade}}</td> ... 
     </tr> 
     </tbody> 
    </table> 
    </div> 
</div> 

而且控制器:

angular.module('sofDataViewerApp') 
    .controller('dataTableController', function ($scope, $firebaseObject, $firebaseArray) { 
    // This gets all of our groups. It also allows `group.memebers` to render the appropriate number of `<tr>`s as it knows it has N students. 
    var groupsRef = new Firebase("https://sof-data.firebaseio.com/studyGroups"); 
    $scope.groups = $firebaseArray(groupsRef); 

    // This will give all the students. How do I get just the template's `group.members` to be each appropriate student? 
    var studentsRef = new Firebase("https://sof-data.firebaseio.com/students"); 
    $scope.allStudents = $firebaseArray(studentsRef); 
    }); 

問題
如何獲得HTML模板適當地迭代OV呃groups'members我可以訪問每個student的所有數據(例如他們的firstNamegrade)? (我假設它可能涉及到控制器,並可能在HTML更好的代碼)

PLNKR:http://plnkr.co/edit/uAn1Ky9v5NHHDFPs2c6S?p=preview (如果你知道如何在plnkr注入火力地堡和angularFire,那麼它應該工作,但我仍然可以」 W圖表示出來,儘管其中包含的CDN ...)

我可以理解最接近的解釋是this page由火力地堡,他們建議使用LINK_ID,但它沒有被描述爲他們的意思,尤其因爲他們聊關於不按id搜索。在我的情況下,link將與group同義,而comment將是student

var commentsRef = new Firebase("https://awesome.firebaseio-demo.com/comments"); 
    var linkRef = new Firebase("https://awesome.firebaseio-demo.com/links"); 
    var linkCommentsRef = linkRef.child(LINK_ID).child("comments"); 
    linkCommentsRef.on("child_added", function(snap) { 
     commentsRef.child(snap.key()).once("value", function() { 
     // Render the comment on the link page. 
     }); 
    }); 

回答

1

它看起來像你試圖加入多個「表」。通過擴展$firebaseArraylink to docs)來實現與AngularFire的最佳方式。使用此功能,您可以在每個孩子添加(或修改)時從其他位置加載其他數據。

另請參閱Kato的this answer顯示擴展數組(儘管用於不同的用例)。

最後,您可以考慮將每個屏幕所需的數據複製到該屏幕的列表中。雖然Firebase非常快速地檢索連接的數據(它保持開放式連接,並且可以傳輸多個請求),但如果您必須執行較少的請求才能獲取數據,它總是會更快。

這方面的一個例子是,如果你想在一組,顯示成員名單:

"group": { 
    "one" : { 
     "members" : { 
     "alexa" : "ReJina", 
     "bobby" : "Robert", 
     "chris" : "Christian" 
     ... 

所以,而不只是存儲true的,我們實際存儲每個成員的名字在這裏。這使我們能夠得到的所有數據,以顯示該組成員在單個讀操作:

ref.child('group/one/members').on('value'... 

然後,當該組成員之一的用戶點擊,我們會去到該組成員的頁面,並加載來自/students/$studentid的數據並且可能具有它們所屬的組(以及那些組的名稱)。