Saving Structured Data In Firebase With Angular
I understand the concepts and best practices of saving structured data in firebase, but I'm not clear on how to go about actually saving the data to multiple locations and providin
Solution 1:
Not sure its the best answer, but I found the following worked for me. It makes use of Frank van Puffelen's suggestion to use Multi-location updates. The following code was setup to support adding a new image, using an existing image or no image at all. I figure the same could apply to any meta data be in categories, tags, or users.
.controller('AddPostController', ['$scope','$firebaseArray','FIREBASE_URI',
function($scope,$firebaseArray,FIREBASE_URI) {
var ref = new Firebase(FIREBASE_URI);
$scope.AddPost = function(){
var newArticleRef = ref.child('articles').push();
var newArticleKey = newArticleRef.key();
var newImageRef = ref.child('images').push();
var newImageKey = newImageRef.key();
// Create the data we want to update
var addNewPost = {};
// Add new article...
addNewPost["articles/" + newArticleKey] = {
title: $scope.article.title,
post: $scope.article.post,
emailId: user,
'.priority': user
};
if ($scope.image) {
// Add new image reference to new article...
addNewPost["articles/" + newArticleKey].image = {};
addNewPost["articles/" + newArticleKey].image[newImageKey] = true;
// Add new image...
addNewPost['images/' + newImageKey] = {
image: $scope.image,
emailId: user,
'.priority': user
};
// Add article reference to new image...
addNewPost['images/' + newImageKey].articles = {};
addNewPost['images/' + newImageKey].articles[newArticleKey] = true;
} else if ($scope.article.image) {
// Add existing image reference to article...
addNewPost["articles/" + newArticleKey].image = {};
addNewPost["articles/" + newArticleKey].image[$scope.article.image] = true;
// Add new article reference to existing image...
addNewPost['images/' + $scope.article.image + '/articles/' + newArticleKey] = true;
}
// Do a deep-path update
ref.update(addNewPost, function(error) {
if (error) {
console.log("Error:", error);
}
});
};
}]);
Post a Comment for "Saving Structured Data In Firebase With Angular"