在 angularFire/Firebase 中创建和添加数组 [英] Creating and adding to arrays in angularFire / Firebase
问题描述
我正在尝试使用 angularjs 和 Firebase 构建一个应用程序,类似于帮助我的同学解决问题的论坛.我也希望人们能够回复"同学遇到的具体问题,所以我在angularjs工厂中创建了一个具有许多值的对象,如下所示:
factory.addError = function() {factory.errors.$add({...回复:[]});};
问题在于 Firebase 不会为占位符保存带有空值的参数,例如上面的参数回复".我曾尝试将占位符值硬编码到数组中,但这似乎是一个非常不完整的解决方案,并且伴随着它自己的一系列问题,我不得不删除 Firebase 中的数据.作为参考,这里是链接控制器中的代码:
$scope.error.reply.$push({名称:$scope.replyName,消息:$scope.replyMessage,时间:(new Date()).toString(),赞成:0});
如何将一个空数组初始化到对象中?$push 会正确使用 Firebase 的命令将其保存到自己的数据集中吗?
首先,这里有一些相关的资源和建议:
资源
- 查看 Firebase 的博客文章,最佳实践:Firebase 中的数组 - 数组是邪恶的".
- 此外,还有 AngularFire 开发指南.
- 以及 AngularJS 提供者的文档.
建议
正如 AngularFire API 文档 所述:><块引用>
有几种强大的技术可以转换由 $firebaseArray
和 $firebaseObject
下载和保存的数据.这些技术只能由了解它们的高级 Angular 用户尝试绕过代码."
将所有这些放在一起,您就可以通过以下方式完成您想做的事情:
- 扩展 AngularFire 服务,
$firebaseArray
和$firebaseObject
. - 遵循 扩展服务.
示例
扩展错误 $firebaseObject
.factory('Error', function(fbUrl, ErrorFactory) {返回函数(错误键){var errorRef;如果(错误键){//按键获取/设置错误errorRef = new Firebase(fbUrl + '/errors/'+errorKey);} 别的 {//创建一个新的错误var errorsListRef = new Firebase(fbUrl + '/errors');errorRef = errorsListRef.push();}返回新的 ErrorFactory(errorRef);}}).factory('ErrorFactory', function($firebaseObject){返回 $firebaseObject.$extend({发送回复:函数(回复对象){if(replyObject.message.isNotEmpty()) {this.$ref().child('replys').push(replyObject);} 别的 {alert("您需要输入一条消息.");}}});})
错误控制器
.controller('ErrorController',function($scope, Error) {//设置空回复消息$scope.replyMessage = '';//创建一个新的错误 $firebaseObjectvar error = new Error();$scope.error = 错误;//发送错误回复$scope.reply = function(){error.sendReply({message:$scope.replyMessage});}})
和String.prototype.isNotEmpty()
String.prototype.isNotEmpty = function() {返回 (this.length !== 0 && this.trim());};
(改编自这个答案)
<小时>希望有所帮助!
I am trying to build an app with angularjs and Firebase similar to a forum for helping my classmates with problems. I also want people to be able to 'reply' to the specific problems the classmates are having, so I create an object with many values in the angularjs factory, like this:
factory.addError = function() {
factory.errors.$add({
...
replies: []
});
};
The problem with this is that Firebase doesn't save parameters with empty values for placeholders, such as the parameter 'replies' above. I have tried to hard code a placeholder value into the array, but that seems like a very patchy solution, and that comes with it's own set of problems for me having to delete out the data in Firebase. For reference, here is the code in the linked controller:
$scope.error.replies.$push({
name: $scope.replyName,
message: $scope.replyMessage,
time: (new Date()).toString(),
upvote: 0
});
How do you initialize an empty array into the object? And will $push properly use Firebase's commands to save it to it's own set of data?
First, here are some relevant resources and suggestions:
Resources
- Check out Firebase's blog post, Best Practices: Arrays in Firebase - "Arrays are evil".
- Also, the AngularFire Development Guide.
- And the documentation for AngularJS providers.
Suggestions
As the AngularFire API Documentation says:
"There are several powerful techniques for transforming the data downloaded and saved by
$firebaseArray
and$firebaseObject
. These techniques should only be attempted by advanced Angular users who know their way around the code."
Putting all that together, you accomplish what you want to do by:
- Extending AngularFire services,
$firebaseArray
and$firebaseObject
. - Following the documentation for extending services.
Example
Extended Error $firebaseObject
.factory('Error', function(fbUrl, ErrorFactory) {
return function(errorKey){
var errorRef;
if(errorKey){
// Get/set Error by key
errorRef = new Firebase(fbUrl + '/errors/'+errorKey);
} else {
// Create a new Error
var errorsListRef = new Firebase(fbUrl + '/errors');
errorRef = errorsListRef.push();
}
return new ErrorFactory(errorRef);
}
})
.factory('ErrorFactory', function($firebaseObject){
return $firebaseObject.$extend({
sendReply: function(replyObject) {
if(replyObject.message.isNotEmpty()) {
this.$ref().child('replies').push(replyObject);
} else {
alert("You need to enter a message.");
}
}
});
})
Error Controller
.controller('ErrorController',function($scope, Error) {
// Set empty reply message
$scope.replyMessage = '';
// Create a new Error $firebaseObject
var error = new Error();
$scope.error = error;
// Send reply to error
$scope.reply = function(){
error.sendReply({message:$scope.replyMessage});
}
})
And String.prototype.isNotEmpty()
String.prototype.isNotEmpty = function() {
return (this.length !== 0 && this.trim());
};
(adapted from this answer)
Hope that helps!
这篇关于在 angularFire/Firebase 中创建和添加数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!