[英]Organize code in javascript Dojo working with XHR, REST
我正在尝试在Dojo中组织我的代码,但是我不了解事情的运作方式。 我想在REST调用后捕获json数据,但是它不起作用。 property, which I'm assigning REST return to, is always NULL. 我为REST返回分配的属性始终为NULL。
我怎样才能做到这一点? 我在下面复制了当前代码。 (我想在ClassDAO和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
});
}
});
});
似乎您正在尝试将类属性设置为变量; 因此,
testJson = jsonData;
必定是:
this.testJson = jsonData
否则,您实际上是在设置全局变量testJson而不是属性。 您还需要将then()函数的作用域限定为您的类:
.then(lang.hitch(this, function(jsonData){
this.testJson = jsonData;
}))
and needs adding to the define() lang对象是 ,需要添加到define()中
define([
"dojo/_base/declare",
"dojo/request/xhr",
"dojo/_base/lang"
], function (declare, xhr, lang) {
范围界定是Java的重要概念,过去曾使我们震惊。 有关更多详细信息,请参见hitch()命令的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.