I am trying to organize my code in Dojo but I, do not understanding how things work. I want to catch the json data after a REST call but it isn’t working. The testJson property, which I’m assigning REST return to, is always NULL.
How can I do this? I’ve copied my current code below. (I want to use my code in a ClassDAO and Controller.
define([
'dojo/_base/declare',
'dojo/request/xhr'
], function (declare, xhr) {
return declare(null, {
testJson: null,
constructor: function(){
},
get: function(){
xhr('/rest/reports', {
method: 'get',
handleAs: 'json',
headers: {
Accept: 'application/json'
}
}).then(function(jsonData){
testJson = jsonData;
}, function(err){
alert(err);
}, function(evt){
// Handle a progress event from the request if the
// browser supports XHR2
});
}
});
});
It looks like you are trying to set a class property as a variable; hence,
ought to be:
Otherwise you are actually setting the global variable testJson, instead of a property. You also need to scope your then() function to your class:
The lang object is dojo/_base/lang and needs adding to the define()
Scoping is an important concept of Javascript that has tripped us all up in the past. See the documentation for the hitch() command for more details.