用户登录网站并尝试创建帖子。每当创建新帖子时,该帖子都会与创建该帖子的用户相关联。
指的是 thinkster.io Tutorial ,它使用 AngularFire 的旧 API。
当使用 AngularFire API v0.8.0 时,添加帖子的这行代码会中断:
user.$child('posts').$child(postId).$set(postId);
具有创建帖子方法的Post Factory (post.js)是:
app.factory('Post',
function ($firebase, FIREBASE_URL, User) {
var ref = new Firebase(FIREBASE_URL + 'posts');
var posts = $firebase(ref).$asArray();
var Post = {
all: posts,
//Starting of create function
create: function (post) {
if (User.signedIn()) {
var user = User.getCurrent(); //Gets the current logged in user
post.owner = user.username;
return posts.$add(post).then(function (ref) {
var postId = ref.name();
user.$child('posts').$child(postId).$set(postId);
//user.$getRecord('posts').$getRecord(postId).$set(postId);
return postId;
});
}
},
//End of create function
Changelog对于 AngularFire 声明
$child()
no longer exists. The data already exists in the parent object and creating additional synchronized children is not efficient and discouraged. Use data transformations, flatten your data, or drop down to the Firebase SDK and use itschild()
method.
我对如何更改代码以配合 API 中的更新感到困惑。
编辑后
这是 getCurrent 方法:
getCurrent: function(){ // retrieves current user
return $rootScope.currentUser;
},
属于user.js Factory:
'use strict';
app.factory('User', function ($firebase, FIREBASE_URL, Auth, $rootScope) {
var ref = new Firebase(FIREBASE_URL + 'users');
var users = $firebase(ref);
var usersdiv = $firebase(ref).$asArray();
var User = {
create: function (authUser, username) {
users[username] = {
md5_hash: authUser.md5_hash,
username: username
};
users.$update(username, {
md5_hash: authUser.md5_hash,
username: username
}).then(function (dataRef) {
dataRef.setPriority(authUser.uid);
setCurrentUser(username);
});
}, // end of create method
findByUsername: function(username){
if(username){
return usersdiv.$getRecord(username);
}
},
getCurrent: function(){ // retrieves current user
return $rootScope.currentUser;
},
signedIn: function(){ //checks if user is signed in
return $rootScope.currentUser !== undefined;
}
}; // end of User
// so that we can pull info about user when logged in
function setCurrentUser (username){
$rootScope.currentUser = User.findByUsername(username);
}
//for logins and refreshes
$rootScope.$on('$firebaseSimpleLogin:login', function(e, authUser){
var queryRef = ref.startAt(authUser.uid).endAt(authUser.uid);
var queryArray = $firebase(queryRef).$asArray();
queryArray.$loaded().then(function() {
setCurrentUser(queryArray.$keyAt(0));
});
});
//logout
$rootScope.$on('$firebaseSimpleLogin:logout', function(){
delete $rootScope.currentUser;
});
return User;
});
最佳答案
您不需要在本地创建一个同步对象($child 曾经做过的事情)只是为了在 Firebase 中设置一个值。您可以随时使用已经创建的 Firebase ref 执行此操作。我不能确切地说出用户的数据结构是什么,因为它没有被包括在内,但是是这样的:
new Firebase(FIREBASE_URL).child('...path/to/posts').child(postId).set(postId);
很可能,这属于您的用户对象,因此在 Post 工厂中,您可以执行类似 user.addPost(postId)
的操作。
关于javascript - 如何在新的 AngularFire v0.8.0 中实现 $child 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25445360/