使用 AngularFire 基于 id 在路径之间加入数据

Joining data between paths based on id using AngularFire

我目前正在开发一个使用 firebase 和 angularJS (ionic) 的应用程序。基本上这是一个汽车管理应用程序,因此您可以让人们与他人共享他们的汽车。我试图尽可能扁平地构建数据以提高效率。我这里的问题是,如果没有问题我可以显示与登录用户共享的不同汽车的列表 car_id,我无法找到一种方法来显示与显示年份的用户共享的汽车列表和模型。

提前感谢您的帮助!

{
"rules": {
    "users": {
        ".write": true,
        "$uid": {
            ".read": "auth != null && auth.uid == $uid"
        },
        "cars": {
          "car_id":true,
          "role":true // Owner, borower...
        }
    },
    "cars": {
      "car_id":true,
      "model":true,
      "year":true
    }
}

}

carapp.controller("carsController", function($scope, $firebaseObject, $ionicPopup, $ionicHistory) {

$ionicHistory.clearHistory();

$scope.list = function() {
  frbAuth = frb.getAuth();
  if(frbAuth) {
    var userObject = $firebaseObject(frb.child("users/" + frbAuth.uid));
    userObject.$bindTo($scope, "user");
    $scope.cars = frb.child("cars");
}}

$scope.createCar = function() {
  $ionicPopup.prompt({
    model: 'Create a new car',
    inputType: 'text'
  })
  .then(function(result) {
    if(result !== "") {
      var newCar = $scope.cars.push({
        model: result
      })
      var newCarId = newCar.key();
      $scope.user.cars.push({car_id: newCarId, role: "owner" });

    } else {
        console.log("Action not completed");
    }
});

}

});

    <div class="list">
     <a ng-repeat="car in user.cars" >
         <h2>{{car.car_id}}</h2> ----> works fine !
<h2>{{car.model}}</h2> ----> How to get this working ?
         <h2>{{car.year}}</h2> ----> How to get this working ?
     </a>
</div>

users/ 路径中,首先按索引而不是数组存储汽车列表。所以你的结构是:

{
   "users": {
      "kato": {
         "cars": {
            "DeLorean": true
         }
      }
   },

   "cars": {
      "DeLorean": {
          model: "DeLorean",
          year: "1975"
      }
   }
}

要使用 AngularFire 加入此活动,您可以使用多种方法。 AngularFire-only 解决方案可能看起来像这样,利用 $extend:

app.factory('CarsByUser', function($firebaseArray) {
   return $firebaseArray.$extend({
     $$added: function(snap) {
        return new Car(snap);
     },

     $$updated: function(snap) {
        // nothing to do here; the value of the index is not used
     },

     $$removed: function(snap) {
        this.$getRecord(snap.key()).destroy();
     },

     // these could be implemented in a manner consistent with the
     // use case and above code, for simplicity, they are disabled here
     $add: readOnly,
     $save: readOnly
   });

  var carsRef = new Firebase(...).child('cars');
  function Car(snap) {
     // create a reference to the data for a specific car
     this.$id = snap.key();
     this.ref = carsRef.child(this.$id);
     // listen for changes to the data
     this.ref.on('value', this.updated, this);
  }

  Car.prototype.updated = function(snap) {
     this.model = data.model;
     this.year = data.year;
  }

  Car.prototype.destroy = function() {
    this.ref.off('value', this.meta, this);
  };

  function readOnly() { throw new Error('This is a read only list'); }
});

app.controller('...', function($scope, CarsByUser, authData) {
   // authenticate first, preferably with resolve
   var ref = new Firebase(...).child(authData.uid);
   $scope.cars = CarsByUser($scope);
});

对于更复杂和优雅的方法,可以利用 NormalizedCollection 并将该引用传递到 AngularFire 数组中:

app.controller('...', function($scope, $firebaseArray) {
  var ref = new Firebase(...);
  var nc = new Firebase.util.NormalizedCollection(
     ref.child('users/' + authData.uid),
     ref.child('cars')
  )
  .select('cars.model', 'cars.year')
  .ref();

  $scope.cars = $firebaseArray(nc);
});