此变量可在onContext函数中访问,但我想将获取的ajax json结果分配给网格的商店对象。 grid就像gride这样的对象:{store:" some-Json"}就像示例
define([
"dojo/_base/declare",
"dojo/when",
"aps/_View",
"aps/xhr"
], function(
declare,
when,
_View,
xhr
) {
var page, grid, self, contextId,myResult,samp;
return declare(_View, {
init: function() {
self = this;
return ["aps/Grid", {
id: "srv_grid",
selectionMode: "multiple",
columns: [{
field: "id",
name: "Id"
}]
},
];
}, // End of Init
onContext: function(context) {
this.grid = this.byId("srv_grid"); //working
xhr.get("/aps/2/resources/" + aps.context.vars.device.aps.id + "/getresource").
then(function(data){
myResult=data;
this.grid.params.store=data; //got error here this.grid is undefined
this.grid.store=data; //got error here this.grid is undefined
}
),
this.grid.refresh();
},
}); // End of Declare
}); // End of Define
init
是第一个先调用的方法,然后是onContext
方法调用..这个单词在该函数中可用,this
变量具有this.grid属性this.grid属性无法在xhr.get()
中访问。xhr.get()
中的属性已更改。我想访问此xhr.get()
函数内的所有属性。因为我想将我的ajax json结果分配给this.grid.store
和this.grid.params.store
属性
答案 0 :(得分:0)
你可以使用
dijit.byId("srv_grid").set("store",data);
或
self.grid.set("store",data); //self contains reference to the entire widget because in init function, you have assigned 'this' to 'self' variable.
'这'总是在这种情况下为您提供属性或对象。因此,在xhr.get中,您可以访问xhr中可用的所有内容,但不能访问其中任何内容。
答案 1 :(得分:0)
您必须使用dojo/_base/lang ->
hitch功能来设置范围,以便执行您的功能。
在你的情况下,this.grid.params...
引用then(xhr),所以它会返回一个未定义的错误
所以你必须在模块的范围内执行你的功能,如下所示:
重要dojo/_base/lang -> lang
onContext: function(context) {
this.grid = this.byId("srv_grid"); //working
xhr.get("/aps/2/resources/" + aps.context.vars.device.aps.id + "/getresource").
then(
lang.hitch(this,function(data){ // bind the function to the actual context (this)
myResult=data;
this.grid.params.store=data; //got error here this.grid is undefined
this.grid.store=data; //got error here this.grid is undefined
this.grid.refresh();
})
);
};