1

Background:

When a delivery is created, an entry will be created under:

1- DELIVERIES->COMPANY->FIREBASEGENERATEDID->DELIVERY DETAILS

To allow users to track their purchases, they will have the delivery company and the unique ID of the delivery added to their profile.

2- USERS->DELIVERYTRACKING->COMPANY(delivery)->FIREBASEGENERATEDID(generated from deliveries above): TRUE

I am able to utilise Firebase.util.intersection to return an array of Firebase references, based on the use case above. However, when bound to a variable in $scope, I am unable to access the specific keys of the child object although I can display the lot to the view.

I've constructed a detailed JSFiddle which should explain the problem much more eloquently that I can by typing.

JSFiddle Link: http://jsfiddle.net/rwk1/8po8t35q/9/

SO required code:

<body ng-app="testApp" ng-controller="TestCtrl">
    <div data-ng-repeat="tDelivery in trackedDelivery | orderByPriority">
      <div class="profile-cards">
        <p>{{tDelivery}}</p>
      </div>
    </div>
    <br/><br/>
    <h5>Question: How do I access the individual delivery details i.e. status?</h5>
</body>

'use strict';

var app = angular.module('testApp', [
    'firebase'
]);

Controller:

app.controller('TestCtrl', function($scope, $firebase){

    var fb = new Firebase('https://so0001.firebaseio.com');
    var trackingRef = fb.child('users/Cara/deliveryTracking');
    var trackingFb = $firebase(trackingRef);

    trackingFb.$on('loaded', function(){
        console.log(trackingFb.$getIndex());
    });

    var trackedDeliveryRefArray = [];

    (function(){

      trackingFb.$on('loaded', function(){
            //console.log(trackingFb.$getIndex());
          angular.forEach($firebase(trackingRef ).$getIndex(), function (k,v){
              var trackedDeliveryRef = new Firebase.util.intersection(fb.child('users/Cara/deliveryTracking/' + k ), 
                                                                fb.child('deliveries' + '/' + k ));
              trackedDeliveryRefArray.push($firebase(trackedDeliveryRef));
          });


      });
    })();

    $scope.trackedDelivery = trackedDeliveryRefArray;




});

Result:

{"-randomFBKey1":{"company1":true,"name":"name value 1","status":"delivery 1 status"}}

{"-randomFBKey3":{"company2":true,"name":"delivery 3","status":"delivery 3 status"}}



Question: How do I access the individual delivery details i.e. status?

Structure:

Firebase structure

Ryan W Kan
  • 380
  • 3
  • 16
  • See http://stackoverflow.com/questions/22135196/how-do-i-loop-through-the-children-of-a-firebase-instance – Frank van Puffelen Sep 04 '14 at 11:17
  • @FrankvanPuffelen Saw that... I think my situation is a bit more complicated as I have an array of firebase references, so the key values that I get from getIndex() is already a layer down. I.e. getIndex() returns company1, company2, but i can't point down to that level without knowing the parent's key. – Ryan W Kan Sep 04 '14 at 12:13
  • Ouch... this works `snapshot.rec.sortedChildKeys[0]`, but it doesn't look good. – Frank van Puffelen Sep 04 '14 at 13:59
  • 1
    This looks like an old version of AngularFire; you should start by noting the version info. Is there a reason that you can't simply put orders and delivery tracking data into their own paths, rather than deeply nested under parent nodes? It would greatly simplify the work here. One shouldn't [nest data just because you can](https://www.firebase.com/docs/web/guide/structuring-data.html). – Kato Sep 04 '14 at 16:02
  • @kato referencing your reply here on firebase data structure: http://stackoverflow.com/questions/16421179/whats-the-best-way-of-structuring-data-on-firebase, I thought this was correct, as a company needs to keep track of it's deliveries as well, so in this case its a simple case of listing all deliveries under their uid->deliveries. Is it not optimal? Also, yea this is 0.7.3. The app is old, and 0.8 is sooo much work to upgrade :( – Ryan W Kan Sep 04 '14 at 21:50
  • Upgrading should generally be a matter of adding $asObject/$asArray to the $firebase constructions. If you are utilizing functionality like $on(...) for anything but $loaded, or $child, then just use $inst().child() or $inst().on() to quickly upgrade. – Kato Sep 07 '14 at 19:03
  • This may give you some ideas for your join issue: http://stackoverflow.com/questions/25512039/using-firebase-util-to-resolve-one-to-many-relationship/25670443#25670443; your structure is very similar although not exact. There is no built-in way to resolve your situation other than restructuring the data to work out of the box with Firebase.util (note that we'll be updating Firebase.util in the next week or two; I'll keep this use case in mind) – Kato Sep 07 '14 at 19:10
  • @Kato Thanks for the tips! Will the new Firebase.util address the use case? Or should I just go ahead and restructure. – Ryan W Kan Sep 08 '14 at 00:52
  • I should think that by looking at the other stackoverflow question, you may be able to fashion something that meets your needs fairly easily. I wouldn't wait as the exact scope of the refesh is not yet solid. – Kato Sep 08 '14 at 02:49

0 Answers0