0

I'm trying to build a function that stores an array of JS objects in a global scope (I want to access this from an external Prototype function). However, when I try to return the 'build' array, the array is undefined (this is probally because I need a proper callback function).

How can I achieve this in a proper way?

function getMyJson(url){
    var request = $.getJSON(url);
    var items = [];

    request.done(function(response) {
        for (var key in response) {
            if (response.hasOwnProperty(key)) {
                var object = {
                    name: response[key].name,
                    id: response[key].id
                }

                items.push(object);
            }
        }

    });

    return items; // This returns 'undefined', probally because the for loop is still running
}

var data = getMyJson('data.json');
console.log(data); // undefined

Thanks in advance

Enzio
  • 377
  • 4
  • 15
  • "probally because the for loop is still running" — No, it's because the HTTP request is still running. – Quentin Nov 29 '15 at 22:29

1 Answers1

0

As others have mentioned, callbacks are the way to go.

function getMyJson(url, callback){
    var request = $.getJSON(url);
    var items = [];

    request.done(function(response) {
        for (var key in response) {
            if (response.hasOwnProperty(key)) {
                var object = {
                    name: response[key].name,
                    id: response[key].id
                }

                items.push(object);
            }
        }

        callback(items);

    });

}

var data = getMyJson('data.json', function(items){

    //items will be defined here.
});
Jordan Soltman
  • 3,795
  • 17
  • 31
  • Thanks for the comment; that really helped me out. However, what would I need to do to access this 'data variable' from an outside prototype function and how do I know the data is set when I try to access it? – Enzio Nov 29 '15 at 22:36
  • What do you mean by "outside prototype function"? Any caller of of this function can provide a callback. – Jordan Soltman Nov 29 '15 at 22:40
  • For example: `Item.prototype.aFunction = function(image){ var thisData = data };` I try to receive the data from the getMyJson function in the thisData. How does 'thisData' know that 'data' is set? – Enzio Nov 29 '15 at 22:48
  • Well, it can check if it's undefined and then busy wait until it isn't, but why not have aFunction make a call to getMyJson itself? – Jordan Soltman Nov 29 '15 at 22:52
  • That's because the aFunction can be called alot of times. And I don't want to request the JSON each time the function is called. – Enzio Nov 29 '15 at 22:56
  • There are a couple of ways you could design it based on what the purpose may be, but Item could store the result after the first call and return the cached copy each subsequent call. – Jordan Soltman Nov 29 '15 at 23:16
  • Yes, but then again; there can be a lot of items and the data from 'items' will be static through the whole project. – Enzio Nov 30 '15 at 08:27