Hello,

I'm trying to implement following scenario: on home action I would like 
first check User location, if it is set then pass it to home controller, 
else go to `findlocation` state and wait until location is resolved. How to 
accomplish specific action in ui-router states, depending on the User 
location?

This is my current application, but I don't know how and which approach 
better to use, resolve or onEnter to achieve desired effect:

var app = angular.module('myApp', ['ngCookies', 'ui.router', 'Devise', 
'ngResource']);


app.config([
    '$stateProvider', '$urlRouterProvider', function ($stateProvider, 
$urlRouterProvider) {
    $stateProvider.state('home', {
        url: '/home',
        templateUrl: 'home/_home.html',
        controller: 'MainCtrl',
        resolve: {
            location: [
                '$state', '$stateParams', 'User', function ($state, 
$stateParams, User) {
                return User.checkLocation().then(function () {
                    $state.go('home');
                }, function () {
                    $state.go('findlocation');
                });
            }]
        },
        onEnter: [
            '$state', 'User', function ($state, User) {
            User.checkLocation().then(function () {
                $state.go('home');
            }, function () {
                $state.go('findlocation');
            });
        }]
    }).state('findlocation', {
        url: '/findlocation',
        templateUrl: 'findlocation.html',
        controller: 'findLocation',
        resolve: {
            location: [
                '$state ', '$stateParams', 'User', function ($state, 
$stateParams, User) {
                return User.findLocation();


            }]
        }
    }).state('login', {
        url: '/login ',
        templateUrl: 'auth/_login.html ',
        controller: 'AuthCtrl',
        onEnter: [
            '$state', 'CustomAuth', function ($state, CustomAuth) {
            CustomAuth.currentUserResource().then(function () {
                $state.go('home');
            });
        }]
    }).state('register', {
        url: '/register',
        templateUrl: 'auth/_register.html',
        controller: 'AuthCtrl',
        onEnter: [
            '$state ', 'CustomAuth', 'User', function ($state, CustomAuth, 
User) {
            User.checkLocation().then(function () {
                CustomAuth.currentUserResource().then(function () {
                    $state.go('home');
                });
            }, function () {
                $state.go('findlocation');
            });


        }]
    });
    $urlRouterProvider.otherwise('home');
}]);
app.factory('User', function ($q, $window, $cookies) {
    return {
        checkLocation: function () {
            var deferred = $q.defer();


            if ($cookies.location) {
                deferred.resolve $cookies.location;
            } else if (this.location) {
                deferred.resolve this.location;
            } else {
                deferred.reject();
            }


            return deferred.promise;
        },
        getLocation: function () {
            var deferred = $q.defer();


            $window.navigator.geolocation.getCurrentPosition(function (
position) {
                this.location = position;
                deferred.resolve(position);
            });
            return deferred.promise;
        }
    };
})


app.factory('CustomAuth', [
    'Auth', 'User', function (Auth, User) {
    Auth['parseUserToResource'] = function (userObj) {
        var userResource = {
            username: "test",
            location: null
        };
        return new User(userResource);
    };
    Auth['currentUserResource'] = function () {
        return this.currentUser().then(function (userObj) {
            return Auth.parseUserToResource(userObj);
        });
    };
    return Auth;
}]);


app.controller('home', function ($scope, User) {}
app.controller('findLocation', function ($scope, User) {}

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to