angularjs - 使用 AngularFire 根据 id 连接路径之间的数据

标签 angularjs firebase

我目前正在开发一个使用 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 的解决方案可能如下所示,利用 $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);
});

关于angularjs - 使用 AngularFire 根据 id 连接路径之间的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40186590/

相关文章:

angularFire 2 的 Firebase Firestore 集合计数

javascript - 在 Angular JS 中,$scope 变量值从 $http 分配

angularjs - 如何在 typescript 中使用 angularjs app.service 和 $q

java - 如何通过 Android 应用程序更新 Firebase 上的数据?

firebase - firebase 中的电子邮件通知

javascript - 使用 firebase 调用数据库中的所有对象

ios - FireBase - 维护/保证数据一致性

javascript - 始终使用 UTC+0 - 使用 javascript/angularjs 修复客户端浏览器上的自定义时区

angularjs - 使用 ANGULARJS $resource 发布一个 JSON 数组

javascript - Angularjs在指令链接函数中设置有效性