Angular + Firebase + AngularFire种子指令ShowAdmin

时间:2014-05-02 03:04:46

标签: angularjs angularjs-directive firebase angularfire

首先,对我的情况有一个前瞻性的描述......我的应用程序正在利用Angular.js和AngularFire在没有后端的情况下运行。我使用Firebase Simple Login进行身份验证,AngularFire-Seed项目提供了一种获取身份验证信息的简单方法。要在应用程序中创建管理员,我将用户的uid存储在我的Firebase中的/ admin中,因此我可以检查/ admin / simpleLogin:45是否存在,例如,查看具有uid simpleLogin:45的用户是否为管理员。

我正在尝试创建一个指令,如果当前用户是我的应用程序的管理员,将导致显示一个元素。我已经写了一个部分有效的指令,而且我很难解决问题。我请求你的帮助,英勇的读者!

这是我的指令代码:

'use strict';

/* Directives */

angular.module('myApp.directives', []).
    directive('appVersion', ['version', function (version) {
        return function (scope, elm, attrs) {
            elm.text(version);
        };

    }])

    .directive('bpmShowAdmin', function ($rootScope, $scope, syncData, waitForAuth) {
        return {
            restrict: 'A',
            compile: function (el, attr) {
                el.addClass('hide');
                waitForAuth.then(function () {
                    console.log('checking for admin rights');
                    var admins = syncData('admins');

                    admins.$on("loaded", function () {
                        var isAdmin = $rootScope.auth.user.uid in admins;
                        if (isAdmin) {
                            console.log('admin rights granted!');

                            el.toggleClass('hide', !isAdmin);
                        }
                    });
                });

                $rootScope.$on("$firebaseSimpleLogin:logout", function () {
                    el.toggleClass('hide', true);
                });
            }
        }
    });

通过做这样的事情来使用该指令:

<li bpm-show-admin>
    ...
</li>

这大部分时间都有效,但我显然不了解编译/链接阶段或类似的事情。当我第一次登录我的应用程序时,它并不总是显示当我以管理员身份登录时应该可见的所有内容。它刷新一两次后就会起作用,所以我会把指令逻辑(编译与链接与控制器)放在一起,存在某种竞争条件或问题。

我一直在使用AngularFire-seed项目的ngShowAuth作为一个例子,我相信这是由臭名昭着的katowulf开发的。 Here's an example of that code

我做错了什么和/或没有理解?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我开始小提起来获得进一步的帮助,但我结束了生气,因为在小提琴中设置我过于复杂的情况并且自己解决了该死的事情。这就是我想出的:

'use strict';

angular.module('bpmWaitForAdmin', [])

    .service('bpmWaitForAdmin', function ($rootScope, syncData, waitForAuth) {
        return {
            init: function (auth) {
                $rootScope.$on('$firebaseSimpleLogin:login', function (e, user) {
                    var admins = syncData('admins');

                    admins.$on("loaded", function () {
                        if (user.uid in admins) {
                            $rootScope.$broadcast('bpmWaitForAdmin:true');
                        }

                        else {
                            $rootScope.$broadcast('bpmWaitForAdmin:false');
                        }
                    });
                });

                $rootScope.$on('$firebaseSimpleLogin:logout', function () {
                    $rootScope.$broadcast('bpmWaitForAdmin:false');
                });
            }
        };
    })

    .directive('bpmShowAdmin', function ($rootScope, $timeout) {
        return {
            restrict: 'A',
            compile: function (element, attributes) {
                element.addClass('hide');

                $rootScope.$on("bpmWaitForAdmin:true", function () {
                    $timeout(function () {
                        element.removeClass('hide');
                    });
                });

                $rootScope.$on("bpmWaitForAdmin:false", function () {
                    $timeout(function () {
                        element.addClass('hide');
                    });
                });
            }
        }
    });