I'm new to jQuery, so I may be missing something, but I'm seeing what seems to be strange behavior using the getJSON method in 1.4.1.
Basically, I attempt to save the results of the call in a global variable or a data object, but it is not available outside the function passed to getJSON until the second time I try to retrieve it.
<script type="text/JavaScript">
function getJSON() {
if ($(window).data('myjson') == undefined) {
$.getJSON('ajax/some_json', function(data) {
$(window).data('myjson', data);
alert( $(window).data('myjson').candy ; // I can get data out here on the initial call first call
});
}
return $(window).data('myjson'); // but returns undefined undefined here until the second call
}
$(document).ready(function(){
$("#link1").click(function() {
$("#candy").text('candy is ' + getJSON().candy);
});
$("#link2").click(function() {
$("#cup").text('it is ' + getJSON().mappy.cup);
});
});
</script>
Is there something about the scoping of the ajax call I'm not understanding? Any ideas at all?
Thanks,
Douglas