Jurjen Folkertsma
Jurjen Folkertsma

Reputation: 272

Custom data tree name in angularfire

I'm using an $routeProvider that makes pages based on entry id's in my firebase array.

.state('app.Ketel', {
          url: '/ketels/:id',
          views: {
            'menuContent': {
              templateUrl: 'templates/ketels/template.html',
              controller: 'ketelsCtrl'
            }
          }
        })




   ketelapp.controller('ketelsCtrl', function ($scope, $routeParams, $stateParams, $state, firebaseService, $firebaseArray, $firebase){

        $scope.ketels = firebaseService.all;
        $scope.ketel = firebaseService.get($stateParams.id - 1);

      })

my firebase data tree

ketels ->
         1 --> name : blablabla;
               brand : bla bla bla;
         2 --> name : blablabla;
               brand : bla bla bla;

now im trying to make an script that adds items to the array in id format

ketelapp.controller('pushCtrl', function ($scope, $firebaseArray, $firebase, $firebaseObject){
      var id = 3;
      var ref = new Firebase("https://url.firebaseio.com" + "/ketels/" + id);
      // create a synchronized array
      $scope.ketels = $firebaseArray(ref);  

      var ketels = $scope.ketels;

      $scope.addMessage = function(name, brand, desc) {


        $scope.ketels.$add({
          name: name,
          brand: brand,
          desc: desc

        });

      };

})

now my data gets added with the correct id but deeper in the data tree it still used an random data key which messes up my code.

ketels ->
         1 --> random key --> name : blablabla;
                              brand : bla bla bla;
         2 --> random key --> name : blablabla;
                              brand : bla bla bla;

is there any way to disable firebase to use their own custom generated data keys?

--edit--

or another way to fix my routing

Upvotes: 1

Views: 58

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599776

Sure.

You're calling $firebaseArray.$add():

$scope.ketels.$add({...

Which means you're asking Firebase to generate a so-called push id.

If you don't want that, you can:

$scope.ketels.$ref().child('your-unique-id').set({...

Note that there are good reasons why Firebase recommends against using array indices in multi-user apps. Ignore those at your own peril.

Upvotes: 1

Related Questions