Background:
I'm building a simple program that uses the simpleWeather.js API to pull in the current temperature to be used by the rest of the program (http://simpleweatherjs.com/).
Updated Code:
All the below code is within an AngularJS controller and the simpleWeather.js file is linked in the index.html file.
var _this = this;
var seasons = null;
function myAjaxCheck(callBack){
$.simpleWeather({
location: 'Calgary, CA',
woeid: '',
unit: 'c',
success: function(weather) {
var temperatureTemp = parseInt(weather.temp);
callBack(temperatureTemp);
}
});
}
var temperature;
myAjaxCheck(function(returnedTemperature){
temperature = returnedTemperature;
if (temperature > 20){
_this.seasons = summerCollection; <-- summerCollection is an array strings
}
});
Alternative that works:
var _this = this;
var seasons = null;
var test = function(){
_this.seasons = summerCollection;
};
test();
UPDATE: Problem:
It seems that I can update the global variable with the member function test() as in the 2nd snippet of code. However, why isn't the global variable updated with the AJAX callback function in the 1st snippet of code?
I have a div in my HTML that uses ng-repeat to spit out all the strings in the summerCollection array. It works with the 2nd snippet of code but does not with the first.
Any help is greatly appreciated!
Any ideas?
Original Problem:
From what I gathered from the web inspector, it appears that the order in which this code loads is (which it should not):
a. $.simpleWeather({...
b. alert(temperature)
c. success: function(weather){...
Any ideas why this is? I know that if I put alert(temperature) in the success:function(weather){} then it'll return the current temperature but this is not what I want.
I ultimately want the variable temperature to hold the current temperature so that I can use this variable in other functions. For instance, I want to be able to set "seasons" to different strings according to the temperature value.
Any help is greatly appreciated!