GeneralBear
GeneralBear

Reputation: 1021

$firebaseArray child records don't have IDs on their child nodes

I'm making a modded version of the angular-fire todo list. This modification includes making sublists and sublists of sublists.

My problem is that when I add my first level of sublists, the sub-objects don't have the $id that I need to affix a next level of sublist.

They don't appear to have the usual rigamarole of firebase properties, just "title" and "completed" status.

I can't figure out why the ng-repeat I have doesn't give me more information, and especially why it works for the top level objects but not further below.

The original addition:

 $scope.addTodo = function(theTodo) {
    var newTodo = theTodo.trim();
    if (!newTodo.length) {
        return;
    }
    $scope.todos.$add({
        title: newTodo,
        completed: false
    });
    $scope.newTodo = '';
    $scope.subtodo = false;
};

The sublist addition:

$scope.addSubList = function(parent, toDo) {
    console.log(parent, toDo)
    var subRef = newRef.child(parent.$id)
    var newArray = $firebaseArray(subRef)
    var newTodo = toDo.trim();
    if (!newTodo.length) {
        return;
    }
    newArray.$add({title: newTodo,
        completed: false
    })
    $scope.sublistExists = true;
    $scope.newTodo = '';
}

Upvotes: 0

Views: 592

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599766

The $firebaseArray object only works its magic on the first-level children under the location that you initialize it with. So the behavior you are seeing is "working as designed".

If you want to handle multi-level collections, you have two options (as far as I can see):

  1. handle the lower levels yourself
  2. store all lists on a single level and then build the multi-level manually by keeping a parentId in each list

I'd recommend going with option 2, since it closer aligns with the Firebase recommendation to prevent unnecessary nesting. See https://www.firebase.com/docs/web/guide/structuring-data.html

Upvotes: 3

Related Questions