1

I have two tables in firebase Departamentos -> parent and Ciudades -> child. When I'm showing the cities in the ng-repeat view, I need replace the $id Departamento to the name in the Departamentos table. How can I do it?


Database:

database


View:

<tr ng-repeat="obj in ciudades | filter:search">
 <td>{{obj.idItem}}</td>
 <td>{{obj.nombreCiudad}}</td>
 <td>{{obj.departamentoEciudad }}</td> <-- replace the id to the name located in 'departamentos' table
</tr>

view

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • don't use screen shots when asking any question, better use a fiddle to get a good response. – Vidya Sagar Jul 20 '15 at 06:23
  • One way would be to extend the `$firebaseArray` so that every time a city is added/modified, you look up its department. See this section of the AngularFire docs: https://www.firebase.com/docs/web/libraries/angular/guide/extending-services.html#section-firebasearray – Frank van Puffelen Jul 20 '15 at 13:42

2 Answers2

0

You will need to do another lookup to the Departamentos table using departamentoEciudad then add it to your ng-repeat object

$scope.ciudades.departamentoObj = $firebaseObject(rootRef.child(departamentoEciudad));

<td>{{obj.departamentoObj.nombreDepartamento }}</td>
sjm
  • 5,378
  • 1
  • 26
  • 37
  • Great, have you some link or documentation that help me, thanks. – Cristian Gartner Jul 20 '15 at 06:30
  • From the AngularFire Documentation and some trial and error, https://www.firebase.com/docs/web/libraries/angular/api.html – sjm Jul 20 '15 at 06:33
  • You've most of the way there already with what you must of coded, you've got a nice denormalized data-model and your retrieving your data from one of the Nodes correctly – sjm Jul 20 '15 at 06:42
  • ReferenceError: departamentoEciudad is not defined – Cristian Gartner Jul 20 '15 at 06:43
  • In my code departamentoEciudad is just a variable for $scope.ciudades.departamentoEciudad. My code is not complete, its dependent on what you've already coded which is hard to determine as you haven't posted your full code – sjm Jul 20 '15 at 06:48
  • Actually I think Im wrong, I said your data is denormalized correctly, ciudades > departamentos is a one to one relationship? To optimize your read performance you should have the data in the same Node – sjm Jul 20 '15 at 07:08
0

I`m happy because this blocker is already solved, i did it:

I added a new field in database called -nombreDepartamento- and when the controller start with a foreach i do a query to replace the $id by departamentos.nombre :)

$scope.replaceRelated = function() {
    urlRefCiudad.once("value", function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
            var key = childSnapshot.key(),
                refKey = new Firebase(urlItemCiudad+'/'+key);

            urlRefCiudad.once("value", function(snapshot) {
                var idDtoEciudad = snapshot.val()[key].departamentoEciudad,
                    urlDtoEciudad = urlRefDto.child(idDtoEciudad);

                urlDtoEciudad.once("value", function(snapshot) {
                    var nombreRelated = snapshot.val().nombreDepartamento;
                    console.log(nombreRelated);
                    refKey.update({ RnombreDepartamento: nombreRelated});
                });
            });
        });
    });
};