ac360
ac360

Reputation: 7835

Angular UI-Router Minification Error - How Can I Change Resolve Syntax To Be String Injection Based?

I'm using Angular UI-Router with a resolve function, but when I minify the resolve function, my whole application breaks because the resolve function syntax is not correct for minification. It needs to be String-Injection based as outlined here. I'm just not sure how to write it. Any suggestions?

// Resolves
var checkAuthentication = function($q, $location, $rootScope, Users) {
    if ($rootScope.user) return true;
    if (!$rootScope.user) {
        var deferred = $q.defer();
        Users.get(null, function(user) {
            if (!user) {
                window.location = '/';
                return false;
            }
            console.log('User fetched: ', user);
            $rootScope.user = user;
            deferred.resolve();
        }, function() {
            window.location = '/';
            return false;
        });
        return deferred.promise;
    }
};

// Routes
angular.module('Dashboard').config(['$stateProvider', '$urlRouterProvider',
    function($stateProvider, $urlRouterProvider) {
        // For any unmatched url, redirect to '/'
        $urlRouterProvider.otherwise('/dashboard');
        // Now set up the states
        $stateProvider
            .state('dashboard', {
                url: '/dashboard',
                templateUrl: 'views/content/dashboard.html',
                resolve: {
                    checkAuthentication: checkAuthentication
                }
            })

Upvotes: 13

Views: 5412

Answers (1)

Kevin B
Kevin B

Reputation: 95047

The way you would normally do this is by passing an array to the resolve:

resolve: {
    welcome: ['$q', function ($q) {
        var def = $q.defer();
        setTimeout(function () {
            def.resolve("Hello World!");
        },500);
        return def.promise;
    }]
}

With that in mind, you can define your var like this:

var welcome = ['$q', function ($q) {
    var def = $q.defer();
    setTimeout(function () {
        def.resolve("Hello World!");
    },500);
    return def.promise;
}]

but that of course isn't really reuseable, so at that point I would suggest moving it to a service.

Upvotes: 23

Related Questions