0

I'm wondering if anyone could help me find a way to use data pulled from an api and stored as a property in one method into another. This is kinda what I'm looking at atm (not the full code but everything that's relevant), I want to use the data saved in the channel method in the populate method. Thank you for any help, I hope that's an obvious enough explanation. :)

  • Fixed :D but I don't think I can answer because the question was apparently a duplicate *

    var viewer = {
    
        init: function () {
    
            var i;
    
            this.url = "https://wind-bow.glitch.me/twitch-api/";
    
            for (i = 0; i < userArray.length; i += 1) {
    
                this.callAPI(userArray[i]);
    
            }
        },
    
        callAPI: function (users) {
    
            var firstCall = $.get(this.url + "channels/" + users, "jsonp");
            var secondCall = $.get(this.url + "streams/" + users, "jsonp");
    
            $.when(firstCall, secondCall).done(this.userData).then(this.populate);
    
        },
    
        userData: function (data, get) {
    
            if (data[0].logo === null) {
                this.avatar = "<img class=\"logo\" src=\"http://via.placeholder.com/50x50\" />";
            } else {
                this.avatar = "<img class=\"logo\" src=\"" + data[0].logo + "\" />";
            }
    
            if (get[0].stream === null || get[0].stream.isUndefined) {
    
                this.status = "Offline"
    
            } else {
    
                this.status = get[0].stream.channel.status;
            }
    
            this.displayName = data[0].display_name;
    
        },
    
        populate: function () {
    
            var list = new List();
    
            list.add(new User(this.avatar, this.displayName, this.status));
    
            list.draw(getWrapper);
    
        }
    
    };
    
    viewer.init();
    

    }());

Danny Pearson
  • 68
  • 1
  • 10
  • You can declare a global variable then assign the `data` value to it in the `channel` method. After that you can access that global variable in your `populate` method – Sagar Jajoriya Oct 05 '17 at 18:03
  • Cheers for the reply, it still doesn't work outside of channel though :/ does it make a difference that all of the code is wrapped in a self executing function? – Danny Pearson Oct 05 '17 at 18:13
  • You've fallen into the classic mistake of async programming. The linked duplicate should explain all your options. – Jamiec Oct 05 '17 at 18:15
  • Cheers :) There's already a callback function running, and I can already retrieve the data from the API I just need to be able to save and share that data from "Channel" to "Populate" – Danny Pearson Oct 05 '17 at 21:03

2 Answers2

-1

Declare one global variable. assign the value from one function and you can able access from another like below.

var GlobalVar;

function Fn1()
{
GlobalVar="test";
}

function Fn1()
{
alert(GlobalVar);
}
  • It doesn't work, possibly because all of the code is wrapped in a self executing function with it being object orientated. – Danny Pearson Oct 05 '17 at 18:15
  • @DannyPearson I hope it will work if you made like below. i have given some sample to use global variable. var GlobalVar; var viewer = {init: function (p) {GlobalVar=p},init2: function () {alert(GlobalVar);} – Natarajan Veerasekaran Oct 05 '17 at 18:24
-1
var viewer = {

    init: function () {

        var holdData;
        var i;

        this.url = "https://wind-bow.glitch.me/twitch-api/";

        for (i = 0; i < userArray.length; i += 1) {

            this.callAPI(userArray[i]);

            this.populate();
        }
    },

    callAPI: function (users) {

        $.get(this.url + "channels/" + users, this.channel, "jsonp");
        $.get(this.url + "streams/" + users, this.isOnline, "jsonp");

    },

    channel: function (data) {

        if (data.logo === null) {
            this.avatar = "<img class=\"logo\" src=\"http://via.placeholder.com/50x50\" />";
        } else {
            this.avatar = "<img class=\"logo\" src=\"" + data.logo + "\" />";
        }

        this.displayName = data.display_name;
        holdData = data;

    },

    isOnline: function () {


    },

    populate: function () {
        //here you can access the variable holdData

        var list = new List();

        list.add(new User(this.avatar, this.displayName, "placeholder"));

        list.draw(getWrapper);

    }

};

viewer.init();
Sagar Jajoriya
  • 2,377
  • 1
  • 9
  • 17
  • Not only will this not solve the problem, but `holdData` isnt even in scope within merthod `channel` – Jamiec Oct 05 '17 at 18:17