AngularFire:如何运行连接查询 [英] AngularFire: how to run join queries

查看:19
本文介绍了AngularFire:如何运行连接查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中工作,其中包含以下实体:区域、消息以及区域和消息之间的链接.在此链接中,我还可以配置一些额外的属性,例如是否可以为一个区域多次显示消息,以及是否有先决条件消息要显示.

I'm working in a project where I have these entities: region, messages and a link between region and messages. In this link I also can configure some extra properties such as if the message can be show more than once for a region and if there is a pre-requisite message to be show before.

我的基本数据库概念:

以下是我制作数据库的方式:

Below is how I'm making my database:

我希望能够显示一个列表,其中包含消息名称、显示标志和先决条件消息的名称(如果已填充).

I would like to be able to show a list with the message name, the show flag and the name of the pre-requisite message, if filled.

我正在使用 $firebaseArray 对象来执行我的常规列表,并且我已经看到有关如何在您拥有这样的结构时执行查询的示例:

I'm using the $firebaseArray object to do my regular lists and I have seen examples on how to do the queries when you have a structure like this:

"region_messages":{  
   "xxxxxxx":true,
   "yyyyyyy":true
}

在我的情况下我该怎么做,我的结构更复杂.我正在尝试使用 angularfire 进行查询,我需要在 Swift 3 中执行相同的操作.

How could I do in my case, my structure is more complex. I'm trying to query using angularfire and I will need to do the same in Swift 3.

感谢您的帮助!

这是我的 json 树,以防您想运行一些测试:

Here is my json tree in case you want to run some tests:

{
  "messages" : {
    "-KXSIeKHTM4lMRbeey2k" : {
      "active" : true,
      "name" : "link",
      "type" : 3,
      "url" : "http://www.google.com"
    },
    "-KXSIi_qw369nfU28lJJ" : {
      "active" : true,
      "name" : "video",
      "type" : 4,
      "url" : "http://www.youtube.com"
    }
  },
  "region_messages" : {
    "-KXfZYP8e--ZUgaVM9iL" : {
      "-KXSIeKHTM4lMRbeey2k" : {
        "pre_requisite_message_id" : "",
        "show_only_once" : false
      },
      "-KXSIi_qw369nfU28lJJ" : {
        "pre_requisite_message_id" : "-KXSIeKHTM4lMRbeey2k",
        "show_only_once" : true
      }
    }
  },
  "regions" : {
    "-KXfZYP8e--ZUgaVM9iL" : {
      "major" : 1,
      "name" : "Region 1"
    }
  }
}

推荐答案

我按照此视频中的说明进行操作我设法解决了这个问题.拨打后续电话对我来说听起来很奇怪,但我想它没有太大问题.功能:

I followed the instruction in this video and I managed to solve the problem. It sounds weird to me to make subsequent calls but I guess there is not much troubles with it. The function:

$scope.getMessagesByRegion = function(regionId){

    var rootRef = firebase.database().ref();
    var regionMessagesRef = rootRef.child("region_messages/"+ regionId);
    $scope.messages_by_region = []; // Here I reset the scope variable

    regionMessagesRef.on('child_added', function(rmSnap) {

        var messageRef = rootRef.child("messages/"+rmSnap.key);
        messageRef.once('value').then(function(msgSnap){

            var msg = {
                key : msgSnap.key,
                name : msgSnap.val().name,
                type : $scope.getTypeName(msgSnap.val().type),
                show_only_once : rmSnap.val().show_only_once,
                pre_requisite_message : rmSnap.val().pre_requisite_message
            }

            $scope.$apply(function(){
                $scope.messages_by_region.push(msg);
            });
        })

    });
}

对于 swift (iOS),我也必须这样做.到目前为止,性能似乎还不错.

For swift (iOS) I had to do the same. The performance seems good so far.

这篇关于AngularFire:如何运行连接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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