user0129e021939232
user0129e021939232

Reputation: 6355

jquery ajax store variable and then retrieve later on

Hi i am using jquery and ajax to retrieve the user id of the logged in user, Im saving this into a variable as I want to be able to do some logic with it later on. however I am having trouble accessing it. My code is below:

$(document).ready(function () {
    var taskBoard = {
       fetch: function (url, data) {

        $('.loading-icon').fadeIn();
        $('.task_board').addClass('loading');


        $.ajax({
            url: url,
            async: true,
            dataType: 'json',
            data: data,
            type: 'POST',
            success: function (json) {
                $('.loading-icon').fadeOut();
                $('#task_board').html($(json.data.taskBoard));
                $('.task_board').removeClass('loading');
                $('.update-results').hide();

            } // end success
        }); //end ajax
    }, //end fetch function

    authUser: function (url, data) {
        $.ajax({
            url: url,
            async: true,
            dataType: 'json',
            data: data,
            type: 'POST',
            success: function (json) {
                $.each($(json), function (index, item) {
                    taskBoard.storeUser(item.id);
                });


            } // end success
        }); //end ajax

    }, //end authUser function

    storeUser: function (param) {
        var authUserId = param;
        return param;
        // if I do an alert here the correct user id is returned.
    },

} //end var taskBoard

      //However if I do an alert here  outside of the var taskBoard I get an undefined. 

     alert(taskBoard.storeUser());
 });

Any ideas how I can get this globally assigned variable outside of this function?

Upvotes: 0

Views: 646

Answers (2)

Mouser
Mouser

Reputation: 13304

change this

storeUser: function (param) {
    var authUserId = param;
    return param;
    // if I do an alert here the correct user id is returned.
},

change to this:

authUserId : null,
storeUser: function (param) {
    if (param)
    {
        this.authUserId = param;
    }
    return this.authUserId;
},

Now the var authUserId will be stored as a property in the taskBoard object. When param is undefined it will return the value unupdated if not it will update it first and then returns it.

A more elegant solution would be to use Object.defineProperty here.

Delete the storeUser property and after the declaration of the taskBoard object add this:

Object.defineProperty(taskBoard, "storeUser", {
    get : function(){ return this.StoreUserVar; },
    set : function(value){ this.StoreUserVar = value; }
});

Now you can assign the userid with:

taskBoard.storeUser = item.id;

//-------- taskBoard object
        success: function (json) {
            $.each($(json), function (index, item) {
                taskBoard.storeUser = item.id;
                doOtherFunction();
            });
//--------

function doOtherFunction()
{
    //the callback function fired from the success.
    alert(taskBoard.storeUser); //this will alert with the value set.
}

Upvotes: 1

jollyjoyce1995
jollyjoyce1995

Reputation: 321

Well if you need a global variable then declare that variable before the document.ready, since variables defined in this function are only valid in this function

Javascript Scope Examples

Upvotes: 0

Related Questions