使用AngularFire / Firebase查询在视图中创建过滤器

时间:2015-03-28 22:25:15

标签: javascript angularjs firebase angularfire

我正在构建一个用户输入品尝的应用程序,这些品尝会显示在几个地方 - 用户的个人品尝页面和“最新品尝”样式页面。

在品尝页面上,当点击一个按钮时,会调用“basicTastings()”函数,这就是我想要查询数据库的地方,以及只有那些具有“类型:基本”的品牌更新视图的地方“(你也是类型:高级)。

在该函数中,我的测试控制台日志正确输出key()是basic / advanced。我丢失的地方然后将该对象推入范围,以便只有那些对象现在通过ng-repeat。

最后 - 以这种方式做到这一点对我来说是否有意义?我正以这种方式接近它,而不是通过jQuery .click()过滤的想​​法,因为我认为如果我在数据库中最终获得大量数据,这是一种更好的方法。

我的数据设置为:

{
  "tastings" : {
    "-JlUCGqbLssTeob7weQI" : {
      "brewdate" : "2015-03-28T07:07:04.880Z",
      "origin" : "Panama",
      "overallrating" : 4.5,
      "roaster" : "Verve",
      "roastname" : "Los Lajones Honey",
      "subdate" : 1427526458869,
      "thoughts" : "Cup Characteristics: This Honey-Process coffee from Los Lajones is abdundantly juicy and filled with lovely stone fruit notes like Bing cherry and apricot. The body is like a perfectly tempered ganache.",
      "type" : "basic",
      "user" : "simplelogin:19"
    }
  },
    "simplelogin:19" : {
      "date" : 1427086513603,
      "email" : "test@test.com",
      "regUser" : "simplelogin:19",
      "username" : "Tester"
    }
  }
}

我列出品酒的控制器是:

myApp.controller('TastingsListController',
  function($scope, $firebaseArray, $rootScope, $location, Authentication, FIREBASE_URL) {

    $scope.$emit('callForAuth'); // Emitter to initiate $onAuth function in Authentication service 

    var ref = new Firebase(FIREBASE_URL + '/tastings');

    var tastingsInfo = $firebaseArray(ref);

    $scope.filterBasic = function() {

        ref.orderByChild("type").on("child_added", function(tasting) {
            console.log(tasting.key() + ' is a ' + tasting.val().type + ' tasting.');
            if(tasting.val().type === "basic") {
                tastingsInfo.push(tasting);
                console.log(tastingsInfo);
            }
    });
    };
  });

最后是相关的ng-repeat和触发功能的按钮:

                <md-button class="md-raised md-accent" ng-click="filterBasic()" flex>
                    Basic
                </md-button>
            </div>
        </md-toolbar>
        <md-content class="md-padding">
            <div class="basiccontainer" ng-repeat="tasting in tastings | orderBy: 'brewdate'">

编辑:我在console.log中获取的数据发生在每次“基本”的品尝之后,这是它应该发生的地方,但它包含整个对象数组,当我希望它只包含对象时,然后我想将该对象放入一个数组中,然后我可以使用ng-repeat进行迭代。

编辑2:我认为我正在接近这一切:

我一直在阅读和重新阅读,我想我现在看到我的方法,基本上,Firebase文档正在说不要做的事情:

  • 关于我的数据结构以及用户为自己的品尝过滤:目前,我没有索引任何含义,因为数据发生变化,我必须遍历所有内容以查看谁拥有什么
  • 在我的情况下,我应该做的是,当用户提交Tasting时,在品尝中有一个用户索引,即使它只是要有一个创建者,也要将品尝的密钥索引到用户,例如:

品尝:

"tastings" : {
    "-JlUCGqbLssTeob7weQI" : {
      "brewdate" : "2015-03-28T07:07:04.880Z",
      "origin" : "Panama",
      "overallrating" : 4.5,
      "roaster" : "Verve",
      "roastname" : "Los Lajones Honey",
      "subdate" : 1427526458869,
      "thoughts" : "Cup Characteristics: This Honey-Process coffee from Los Lajones is abdundantly juicy and filled with lovely stone fruit notes like Bing cherry and apricot. The body is like a perfectly tempered ganache.",
      "type" : "basic",
        "user" : {
           "simplelogin:18": true,
        },
    },

用户:

    "users" : {
        "simplelogin:18" : {
          "date" : 1427062799596,
          "email" : "test@test.com",
          "regUser" : "simplelogin:18",
          "username" : "Tester",
            "tastings" : {
               "-JlUCGqbLssTeob7weQI": true,
               "<Tasting 2 Key>": true,
               ...
             },          
        },

这个设置将允许我在品尝和它的创建者之间实际建立双向关系。当用户按下“提交”按钮时,我可以通过相同的提交功能完成此操作 - 发送品尝数据,并将品尝的密钥用于用户的品尝索引。

  • 关于我的控制器用户与服务的对象:我现在正在做的是尝试在控制器中执行所有操作。
  • 当我通过按钮按下调用控制器中的功能时,我应该从控制器调用“listingfilter”服务(或者我决定调用的任何服务)。该服务本身应该执行查询Firebase列表的实际工作,将它们放入某种类型的数组中,并将其发送回控制器,然后控制器将该数据提供给$ scope,通过该范围重复将迭代。

0 个答案:

没有答案