Work Journal 2013-jan-10
Javascript is hard!
or maybe just different.
Although I spend most the of morning trying to figure out have to set a global variable based on a synchronous ajax call in javascript I actually progressed a fair bit on my current project. I most of all learned that there is still really much I don’t understand and need to learn about javascript.
I still don’t know why I couldn’t make it work. Maybe because a change in jQuery.
I ended up using the jQuery.getJSON()
and just calling all code in the callback that depended on the data I wanted to put in the global variable.
So in stead of
var globalVariable; // Following code inside the scope of a closure: jQuery.ajax({ url: 'servicehost.net/ressource', success: function (result) { globalVariable = result.relavantData; }, async: false }); // function calls using globalVariable
</code>
I ended up with
// Following code inside the scope of a closure: jQuery.getJSON( 'servicehost.net/ressource', function (result) { var localVariable = result.relavantData; // loads of functioncalls that depend on localVariable } });
</code>
Probably what I ended up with is a better pattern anyway.
Work Journal – a diary on what I did work/programming related today.