0

The script below returns undefined, if I replace var player; with var player = 'hello' then it returns hello. I must be doing something stupid but I can't put my finger on it.

I already know the success function of the post is being triggered because the DOM manipulation is being done, but player doesn't change value.

// js/controllers/authentication.js

define([
    'jquery',
    'jquerym',
    'jqueryck',
    'underscore',
    'backbone'
], function($, JQM, JQCK, _, Backbone) {
    var AuthenticationController = function() {
        var player;
        // Verify signed in player
        if (!_.isUndefined($.cookie('screen')) && !_.isUndefined($.cookie('token1')) && !_.isUndefined($.cookie('token2'))) {
            var screen = $.cookie('screen');
            var token1 = $.cookie('token1');
            var token2 = $.cookie('token2');
            $.post('/api/player-check.php', {screen: screen, token1: token1, token2: token2}, function(data) {
                if (_.isEqual(data.status, 'verified')) {
                    // Update main nav
                    if (!_.isElement($('#site-nav .ui-panel-inner .signed-in-cta')[0])) {
                        $('#site-nav .ui-panel-inner .sign-in-cta').remove();
                        $('#site-nav .ui-panel-inner').prepend('<div class="signed-in-cta row cf"><img class="col" src="http://example.com/'+data.avatar+'"><div class="links col span_13"><a id="profile-lk" class="screen-name col span_12">'+data.screen+'</a><a id="qr-lk" class="qr-code col span_4"><i class="icon icon-qrcode icon-lg"></i></a></div></div>');
                        if (!_.isElement($('#site-nav .ui-panel-inner ul li #sign-out')[0])) {
                            $('#site-nav .ui-panel-inner ul').append('<li><a id="sign-out" class="ui-link"><i class="icon icon-power icon-lg"></i> Sign Out</a></li>');
                        }
                    }
                    player = 'verified';
                } else {
                    player = 'unknown';
                }
            }, 'json');
        } else {
            player = 'unknown';
        }
        return player;
    };
    var authenticated = AuthenticationController();
    console.log(authenticated);
});
jstudios
  • 856
  • 1
  • 9
  • 26
  • 5
    `$.post()` is an asynchronous method -- the callback function you give is executing after the outer function completes. It sets the `player` variable, but since the outer function has already returned this has no effect. – cdhowie Sep 05 '14 at 18:19
  • I figured that'd be an issue. How can I get it to wait for the post to finish? I'm thinking making a separate function that set's the value and calling it inside the post success? – jstudios Sep 05 '14 at 18:25
  • it may be that _.isEqual(data.status, 'verified') always return false – Mukund Kumar Sep 05 '14 at 18:25
  • 1
    jstudios: That's the best solution. Have $.post call another function with the result when it's finished. This is a common pattern in javascript. – EricP Sep 05 '14 at 18:26
  • You know what, I noticed it's obviously safer for me to check if the player is signed in when I make an API call that returns sensitive information. I will be sending the authentication cookies through POST to a php file and do the check there. I will keep this controller for the purposes of updating the menu with the avatar and profile link. Thanks for the help! – jstudios Sep 05 '14 at 18:50

0 Answers0