$ scope var未在Parse更新

时间:2016-01-27 23:32:19

标签: angularjs parse-platform ionic-framework angularjs-scope

我正在使用离子和解析来构建应用程序。我正在基于点击更新解析中的布尔值。一切都在解析结束时工作,我看到在函数运行后在控制台中更新了用户对象,但是在用户注销,返回到页面之前,范围变量不会更新,然后通常甚至必须再次刷新才能看到$ scope.isInstagramLinked已更新为其真实值。

控制器

var app = angular.module('myApp.controllers.account', []);

app.controller('AccountCtrl', function ($scope, $state, $cordovaOauth, AuthService) {

    $scope.isInstagramLinked = AuthService.user.attributes.is_instagram_linked;

    $scope.linkInstagram = function() {
        $cordovaOauth.instagram('######', [], {})
            .then(function(result) {
                console.log("Response Object -> " + JSON.stringify(result));
                    console.log(result.access_token);

                    // save the access token & get user info
                    AuthService.setInstagramAccessToken(result.access_token).then(function() {
                        console.log('Token saved!');
                    });
            }, function(error) {
                console.log("Error -> " + error);
            });
    }

    $scope.unlinkInstagram = function() {
        AuthService.removeInstagramInfo().then(function() {
            console.log('Insta unlinked');
            console.log(AuthService.user.attributes);
        });
    }
});

服务

  var app = angular.module('myApp.services.authentication', []);

    app.service('AuthService', function ($q, $http, $ionicPopup) {
        var self = {
            user: Parse.User.current(),
            'setInstagramAccessToken': function(token) {
                var d = $q.defer();

                var user = self.user;

                user.set("instagram_access_token", token);

                user.save(null, {
                    success: function(user) {
                        self.user = Parse.User.current();
                        d.resolve(self.user);
                    },
                    error: function(user, error) {
                        $ionicPopup.alert({
                            title: "Save Error",
                            subTitle: error.message
                        });
                        d.reject(error);
                    }
                });

                self.setInstagramUserInfo(token);

                return d.promise;
            },
            'setInstagramUserInfo': function(token) {
                var d = $q.defer();

                var endpoint = 'https://api.instagram.com/v1/users/self?access_token=' + token + '&callback=JSON_CALLBACK';

                $http.jsonp(endpoint).then(function(response) {
            console.log(response.data.data.username);
                    console.log(response.data.data.id);

                    var user = self.user;

                    user.set('is_instagram_linked', true);
                    user.set('instagram_username', response.data.data.username);
                    user.set('instagram_user_id', response.data.data.id);

                    user.save(null, {
                        success: function(user) {
                            self.user = Parse.User.current();
                            d.resolve(self.user);
                        },
                        error: function(user, error) {
                            $ionicPopup.alert({
                                title: "Save Error",
                                subTitle: error.message
                            });
                            d.reject(error);
                        }
                    });
            });
            },
            'removeInstagramInfo': function() {
                    var d = $q.defer();

                    var user = self.user;

                    user.set('is_instagram_linked', false);
                    user.set('instagram_access_token', null);
                    user.set('instagram_username', null);
                    user.set('instagram_user_id', null);

                    user.save(null, {
                        success: function(user) {
                            self.user = Parse.User.current();
                            d.resolve(self.user);
                        },
                        error: function(user, error) {
                            $ionicPopup.alert({
                                title: "Save Error",
                                subTitle: error.message
                            });
                            d.reject(error);
                        }
                    });

                    return d.promise;
            }

        };

        return self;
    });

我在函数结束时尝试了类似这样的东西,但得到错误说错误:[$ rootScope:inprog] $ digest已在进行中

$scope.$apply(function () {
     $scope.isInstagramLinked = false;
});

1 个答案:

答案 0 :(得分:0)

我猜你正在假设以下一行

    $scope.isInstagramLinked = AuthService.user.attributes.is_instagram_linked;

将制作' $ scope.isInstagramLinked'随时更新' AuthService.user.attributes.is_instagram_linked'更新。但事实并非如此。因为' AuthService.user.attributes.is_instagram_linked'引用一个原始(布尔)值,它只是指定它 - 它不维护任何类型的引用 - 只发生在对象上。

您需要在$ cordovaOauth.instagram()成功/"然后"中手动设置$ scope.isInstangramLinked = true。处理程序。

TL; DR:

$scope.isLinked = false;

someFunction().then(function(){
   $scope.isLinked = true; // this is what you're missing
})
.error(function(err){...})

如果您不想手动设置,也可以使用$scope.$watch观看AuthService.user.attributes.is_instagram_linked'对于更改,然后更新' $ scope.isInstagramLinked'什么时候。

相关问题