Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
                }
            })
like image 231
ac360 Avatar asked Jul 08 '14 15:07

ac360


1 Answers

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.

like image 109
Kevin B Avatar answered Sep 27 '22 22:09

Kevin B