如何使用火力地堡查询在角工厂吗? [英] How to use Firebase query in Angular factory?

查看:126
本文介绍了如何使用火力地堡查询在角工厂吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我在工厂工作的新火力地堡查询,但我坚持。我想回到标识的值超出我的对手表的基础上,对手的名字。我可以从火力地堡文档获取运行与的console.log 的例子:

I'm trying to get the new Firebase queries working in my factory, but I'm stuck. I want to return the value of 'logo' out of my opponents table, based on the opponent name. I can get the example with console.log from the Firebase docs running:

function OpponentService($firebase) {
    var factory = {};
    var _url = 'https://somefirebaseproject.firebaseio.com/opponents';
    var _ref = new Firebase(_url);

    factory.getLogo = function(opponent) {
        _ref.orderByChild('name').equalTo(opponent).on("child_added", function(snapshot){
            console.log(snapshot.val().logo);
        });
    };

    return factory;
}

当我把我与工厂OpponentService.getLogo(opponentName)在我的控制器,我得到了我的控制台正确的标识ID。但我怎么能返回值,而不是将其发送到我的控制台呢?我希望将其存储在这样的变量: $ scope.opponentLogo = OpponentService.getLogo(opponentName)。我试图用一个return语句如下几个变化:

When I call my factory with OpponentService.getLogo(opponentName) in my controller, I get the correct logo id in my console. But how can I return the value instead of sending it to my console? I want to store it in a variable like this: $scope.opponentLogo = OpponentService.getLogo(opponentName). I tried several variations with a return statement like this:

factory.getLogo = function(opponent) {
        _ref.orderByChild('name').equalTo(opponent).on("child_added", function(snapshot){
            return snapshot.val().logo;
        });
};

但显然我不完全了解工厂角是如何工作的,因为我得到一个未定义。难道该值尚不可用,我应该使用以某种方式承诺?任何人谁可以点我在正确的方向?

But apparently I don't fully understand how factories work in Angular, because I get an undefined. Could it be that the value isn't available yet and I should use a promise in someway? Anyone who can point me in the right direction?

推荐答案

您是从火力地堡上()调用内部的匿名函数返回的标志的价值,但你不能从 getLogo返回任何东西()

You're returning the value of logo from the anonymous function inside the Firebase on() call, but you're not returning anything from getLogo().

返回的承诺会做到这一点的好办法。这是你如何检索与 AngularFire 对手的标志,如果有难保 opponentName 将是独一无二的:

Returning a promise would be a good way to do this. This is how you retrieve the opponent logo with AngularFire, if there is no guarantee opponentName will be unique:

// getLogo() returns a promise that you can bind to the UI.
// When loading finishes, the binding will get the opponent's logo value.
factory.getLogo = function (opponentName) {
    var opponentsArray = $firebase(_ref.orderByChild('name').equalTo(opponentName)).$asArray();

    // $loaded() returns a promise, so we can use the then() method to add
    // functionality when the array finishes loading and the promise resolves.
    var r = opponentsArray.$loaded().then(function () {
        // Now return the logo for the first matching opponent
        return opponentsArray[0].logo;
    });

    return r;
};

如果 opponentName 的唯一,我会重新考虑数据结构,使您可以使用opponentName作为对手的关键。然后,你将保证得到一个对手,可以与获取它:

If opponentName is unique, I would rethink the data structure so that you could use the opponentName as the key for opponents. Then you would be guaranteed to get a single opponent and could fetch it with:

var opponent = $firebase(_ref.child(opponentName)).$asObject();

如果你不使用AngularFire,您可以返回采用了棱角分明的 $ Q 服务承诺。下面应该工作:

If you're not using AngularFire, you can return a promise using Angular's $q service. The following should work:

factory.getLogo = function(opponent) {
    $q(function (resolve, reject) {
        function successCallback(snapshot) {
            resolve(snapshot.val().logo);
        };

        function cancelCallback(error) {
            reject(error);  // pass along the error object
        };

        _ref.orderByChild('name').equalTo(opponent)
            .on("child_added", successCallback, cancelCallback);
    });
};

您可以指定的结果getLogo()来一个范围变量,当火力地堡返回值绑定将在UI更新。

You can assign the result of getLogo() to a scope variable, and bindings will update in the UI when Firebase returns the value.

这篇关于如何使用火力地堡查询在角工厂吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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