使用restangularpromise在angularJS中创建一个服务

creating a service in angularJS, using restangular promises

本文关键字:一个 服务 创建 restangularpromise angularJS 使用      更新时间:2023-09-26

好吧,这让我很生气,基本上我要做的是创建一个服务来获取和评估用户功能,我使用的是WPRESTneneneba API。我使用restangular来获取JSON数据。

在这个阶段,我在控制器本身中测试函数,但无论我在哪里测试它,无论是在我的自定义服务中使用this.method还是在控制器内部,无论是否使用$scope,结果总是未定义的。我知道我在函数内部返回true或false的方式中遗漏了一些东西,或者在JS中的promise中有一些根本不同的东西。这是代码:

    var current_user = parseInt(o2_i18n.user_id),
        currentUserCapabilities,
        capability;
    $scope.currentUserCan = function(capability) {
        if(current_user !== '0') {
            wpAPIResource.one('users').get()
            .then(function(allUsers){
                for (var i = 0; i < allUsers.length; i++) {
                    if ( allUsers[i].id === current_user ) {
                        var currentUserCapabilities = allUsers[i].capabilities;
                        for(var prop in currentUserCapabilities){
                            if (capability === prop) {
                                //$log.log( prop );
                                return prop;
                            } else {
                                //$log.log( prop );
                                return false;
                            }
                        }
                    }
                }
            }, function(reason){
                $log.error(reason);
            });
        } else {
            //The user is not logged in, therefor no capabilities
            return false;
        }
    };
    $log.log($scope.currentUserCan('publish_posts'));
    if ( $scope.currentUserCan('publish_posts') ) {
        $log.log( 'Yes I Can!' );
    } else {
        $log.warn('No Can''t Do!');
    }

如果current_user !== '0',则currentUserCan函数不会返回任何内容。例如,您应该让它返回一个promise(对于以下内容,您需要注入$q服务)

$scope.currentUserCan = function(capability) {
    if(current_user !== '0') {
        // note the "return" here
        return wpAPIResource.one('users').get().then(function(allUsers){
            for (var i = 0; i < allUsers.length; i++) {
                if ( allUsers[i].id === current_user ) {
                    var currentUserCapabilities = allUsers[i].capabilities;
                    for(var prop in currentUserCapabilities){
                        if (capability === prop) {
                            return prop;
                        }
                    }
                }
            }
            return false;
        }, function(reason){
            $log.error(reason);
            return $q.reject(reason); // you still want the promise to fail
        });
    } else {
        return $q.resolve(false);
        // this turns the static value into a promise so the API for this
        // function is consistent
    }
};

然后消耗类似的功能

$scope.currentUserCan('publish_posts').then(function(can) {
    if (can) {
        $log.log('Yes I Can!');
    } else {
        $log.warn("No Can't Do!");
    }
});

我还稍微清理了一下你的环。在OP中,如果在allUsers数组中找不到用户,那么内循环中没有点,也就没有return值。