检索与查询记录,AngularFire [英] Retrieve record with query, AngularFire

查看:122
本文介绍了检索与查询记录,AngularFire的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用火力地堡,后端,作为一种服务,为我的网站的应用程序,通过编程AngularJS。

我想使用的数据库查询获取记录,如:其中name =='的传说Korra

我在火力地堡服务器电视剧的阵列, JSON code

  {
  系列:{
    -JkqSvwyDw5F9DvGUu6b:{
      createdAt:1426842959884,
      创造者:迈克尔·但丁DiMartino,布莱恩Konietzko
      说明:该系列遵循主角十二岁的阿昂和他的朋友们的冒险,谁必须对其他三个国家结束了火焰领主的战争带来和平与团结的世界
      episode_reset:真实,
      流派:动作,冒险,奇幻,喜剧片
      关键词:阿凡达最后的气宗,
      名:降世神通:最后的气宗,
      official_website:http://nicktoons.nick.com/shows/avatar,
      出版日期:2005-02-21,
      updatedAt:1426843055127
    }
  }
}

在code,我现在使用,使这个没有工作:

  $ scope.isSeriesValid =功能(SERIESNAME){
    VAR seriesRef =新的火力地堡(的http://<< firebaseapp>> .firebaseio.com /系列');
    VAR的SeriesCollection = $ firebaseArray(seriesRef);
    的SeriesCollection $加载(函数(){
        angular.forEach(的SeriesCollection,功能(seriesObject,指数){
            如果(seriesObject.name == SERIESNAME)
                返回true;
        });
    });
    返回false;
};

我想使这个失去工作,而无需使用 $ firebaseArray 的forEach 。我应该怎样做呢?

就这样的事情:

  VAR seriesRef =新的火力地堡(的http://<< firebaseapp>> .firebaseio.com /系列');
VAR seriesObject = $ firebaseObject(seriesRef.query({名称:SERIESNAME}));


解决方案

我有一些建议,可以帮助在这里:

示例


  • 看看这个工作 PLNKR例如

    • 我在公共火力地堡实例之一复制您的数据。

    • 您正在寻找的是 seriesCollectionRef.orderByChild(name)的查询。equalTo(SERIESNAME)

    • 如果您输入降世神通:最后的气宗。在输入并单击查找,您将获得匹配的一系列对象


  • 在我的例子,我延长了 $ firebaseArray 服务,包括寻找某个名字的一系列的方法。

    • 查看文档<一个href=\"https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-extending-the-services\"相对=nofollow>延长AngularFire服务。

    • 您可以完成同样的事情不会延长服务,请参阅最后code片段。


工厂

app.factory('SeriesFactory',函数(SeriesArrayFactory,fbUrl){
  返回功能(){
    VAR REF =新的火力地堡(fbUrl +'/系列');
    返回新SeriesArrayFactory(REF);
  }
});app.factory('SeriesArrayFactory',函数($ firebaseArray,$ Q){
  返回$ firebaseArray。$扩展({
    findSeries:功能(SERIESNAME){
      变种推迟= $ q.defer();
      //查询由'名'
      这一点。$ REF()。orderByChild(名)。equalTo(SERIESNAME)。一旦(价值,功能(dataSnapshot){
        如果(dataSnapshot.exists()){
          deferred.resolve(dataSnapshot.val());
        }其他{
          deferred.reject(未找到。);
        }
      });
      返回deferred.promise;
    }
  });
});

控制器

app.controller(HomeController中',函数($范围,SeriesFactory,fbUrl){  $ scope.seriesName ='';  $ scope.findSeries =功能(){
    的console.log(寻找系列名称:,$ scope.seriesName,');
    VAR的SeriesCollection =新SeriesFactory();
    seriesCollection.findSeries($ scope.seriesName)。然后(功能(数据){
      的console.log(发现的数据);
      $ scope.series =数据;
    })赶上(功能(错误){
      console.warn(错误);
    });
  };});

无扩展服务

下面是一个控制器的功能是什么样子,如果你不使用工厂:

  $ scope.findSeriesWithoutFactory =功能(){
    VAR seriesRef =新的火力地堡(fbUrl +'/系列');
    VAR的SeriesCollection = $ firebaseArray(seriesRef);
    的SeriesCollection。$ REF()。orderByChild(名)。equalTo($ scope.seriesName)。一旦(价值,功能(dataSnapshot){
        VAR系列= dataSnapshot.val();
        如果(dataSnapshot.exists()){
          的console.log(发现系列);
          $ scope.series =系列;
        }其他{
          console.warn(未找到。);
        }
    });
  };

规则

注意:请注意,您应该indexOn。增加是很重要的:姓名来的火力地堡规则,使查询运行高效。像这样:

yourfirebaseapp:{
    .read:......
    .WRITE:......
    系列:{
      .indexOn:名
    }
  }

希望帮助!

I'm using Firebase, Backend-as-a-Service, for my website application, programming by AngularJS.

I want to use db queries to fetch records, like: WHERE name == 'The Legend Of Korra'

I have array of TV Series in the Firebase server, JSON CODE:

{
  "series" : {
    "-JkqSvwyDw5F9DvGUu6b" : {
      "createdAt" : 1426842959884,
      "creators" : "Michael Dante DiMartino,Bryan Konietzko",
      "description" : "The series follows the adventures of protagonist twelve-year-old Aang and his friends, who must bring peace and unity to the world by ending the Fire Lord's war against the other three nations.",
      "episode_reset" : true,
      "genres" : "Action,Adventure,Fantasy,Comedy-drama",
      "keywords" : "avatar,the last airbender",
      "name" : "Avatar: The Last Airbender",
      "official_website" : "http://nicktoons.nick.com/shows/avatar",
      "publish_date" : "2005-02-21",
      "updatedAt" : 1426843055127
    }
  }
}

The code I'm using now to make this out work:

$scope.isSeriesValid = function(seriesName) {
    var seriesRef = new Firebase('http://<<firebaseapp>>.firebaseio.com/series');
    var seriesCollection = $firebaseArray(seriesRef);
    seriesCollection.$loaded(function() {
        angular.forEach(seriesCollection, function(seriesObject, index) {
            if(seriesObject.name == seriesName)
                return true;
        });
    });
    return false;
};

I want to make this out work without using $firebaseArray and forEach. How should I make it?

Just something like this:

var seriesRef = new Firebase('http://<<firebaseapp>>.firebaseio.com/series');
var seriesObject = $firebaseObject(seriesRef.query({ name: seriesName }));

解决方案

I have some suggestions that may help here:

Example

  • Check out this working PLNKR example.
    • I replicated your data in one of my public Firebase instances.
    • The query that you're looking for is seriesCollectionRef.orderByChild("name").equalTo(seriesName)
    • If you enter 'Avatar: The Last Airbender' in the input and click "Find", you'll get the matching series object.
  • In my example, I extended the $firebaseArray service to include a method for finding a specific series by name.

Factories

app.factory('SeriesFactory', function(SeriesArrayFactory, fbUrl){
  return function(){
    var ref = new Firebase(fbUrl+'/series');
    return new SeriesArrayFactory(ref);
  }
});

app.factory('SeriesArrayFactory', function($firebaseArray, $q){
  return $firebaseArray.$extend({
    findSeries:function(seriesName){
      var deferred = $q.defer();
      // query by 'name'
      this.$ref().orderByChild("name").equalTo(seriesName).once("value", function(dataSnapshot){
        if(dataSnapshot.exists()){
          deferred.resolve(dataSnapshot.val());
        } else {
          deferred.reject("Not found.");
        }
      });
      return deferred.promise;
    }
  });
});

Controller

app.controller('HomeController',function($scope, SeriesFactory, fbUrl) {

  $scope.seriesName = '';

  $scope.findSeries = function() {
    console.log("Finding series with name:'",$scope.seriesName,"'");
    var seriesCollection = new SeriesFactory();
    seriesCollection.findSeries($scope.seriesName).then(function(data){
      console.log("Found",data);
      $scope.series = data;
    }).catch(function(error){
      console.warn(error);
    });
  };

});

Without Extended Service

Here is what a controller function would look like if you weren't using the factories:

  $scope.findSeriesWithoutFactory = function() {
    var seriesRef = new Firebase(fbUrl+'/series');
    var seriesCollection = $firebaseArray(seriesRef);
    seriesCollection.$ref().orderByChild("name").equalTo($scope.seriesName).once("value", function(dataSnapshot){
        var series = dataSnapshot.val();
        if(dataSnapshot.exists()){
          console.log("Found", series);
          $scope.series = series;
        } else {
          console.warn("Not found.");
        }
    });
  };

Rules

Note: It's important to note that you should add ".indexOn":"name" to your Firebase rules so that the query runs efficiently. Like so:

  "yourfirebaseapp": {
    ".read": "...",
    ".write": "...",
    "series": {
      ".indexOn": "name"
    }
  }

Hope that helps!

这篇关于检索与查询记录,AngularFire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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