gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:09:01 24 4
gpt4 key购买 nike

我目前正在开发一个使用 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/41530784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com