创建和angularFire /火力地堡加入阵列 [英] Creating and adding to arrays in angularFire / Firebase

查看:153
本文介绍了创建和angularFire /火力地堡加入阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立与angularjs一个应用程序和火力地堡类似帮助我的同学有问题的论坛。我也希望人们能够回复到同学们遇到的具体问题,所以我在angularjs工厂创造了许多值的对象,像这样的:

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: []
 });
};

这样做的问题是,火力地堡不保存,如上面的参数'回复'占位符为空值,参数。我试图硬code的占位符值到数组,但这似乎是一个非常零散的解决方案,并附带它自己的一套具有火力地堡删除了数据,我的问题。作为参考,这里是code中的链接控制器:

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:

  • 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.

由于 AngularFire API文档说:

有转换由 $ firebaseArray 下载并保存数据和 $ firebaseObject 。这几个强有力的技术技术只能由谁知道他们围绕code方式先进角的用户尝试使用。

"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:


  • 扩展AngularFire服务, $ firebaseArray 和的 $ firebaseObject

  • 以下为 <一个文档href=\"https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-extending-the-services\"相对=nofollow>扩展服务

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

错误控制器

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

String.prototype.isNotEmpty()

String.prototype.isNotEmpty = function() {
    return (this.length !== 0 && this.trim());
};

(改编自这个答案

这篇关于创建和angularFire /火力地堡加入阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆