一尘不染

使用AngularFire在基于id的路径之间联接数据

angularjs

我目前正在使用Firebase和angularJS(离子)开发应用程序。基本上,这是一个汽车管理应用程序,因此您可以与其他人共享汽车。我试图将数据结构尽可能平坦以提高效率。我的问题是,如果可以毫无问题地显示与登录用户共享的不同汽车的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>

阅读 248

收藏
2020-07-04

共1个答案

一尘不染

users/路径中,首先按索引而不是数组存储汽车列表。因此,您的结构将是:

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

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

要使用AngularFire加入此方法,您可以使用几种方法。仅限AngularFire的解决方案可能看起来像这样,它利用了$
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);
});
2020-07-04