本文介绍了基于使用AngularFire标识接合路径之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的工作应用程序使用火力和angularJS(离子)。基本上,这是一个汽车的管理应用程序,让你拥有人与他人分享他们的汽车。我试图结构尽可能平坦的数据是有效的。我在这里的问题是,如果没有问题,我可以显示与登录用户共享不同车的car_id名单,我无法找到一个方法来显示与显示年份和模型用户共享汽车的名单。

感谢您提前为您的帮助!

  {
规则:{
    用户:{
        .WRITE:真实,
        $ UID:{
            .read:AUTH = NULL&放大器;&安培; auth.uid == $ UID
        },
        汽车: {
          car_id:真实,
          角色:真//所有者,borower ...
        }
    },
    汽车: {
      car_id:真实,
      模式:真实,
      年:真实
    }
}

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 !
     </a>
</div>
解决方案

In the users/ path, begin by storing the list of cars by index, instead of in an array. So your structure would be:

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

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

To join this using AngularFire, you have several approaches available. An AngularFire-only solution might look like this, taking advantage of $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);
});

For a more sophisticated and elegant approach, one could utilize NormalizedCollection and pass that ref into the AngularFire array:

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);
});

这篇关于基于使用AngularFire标识接合路径之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:39