2017-09-20 43 views
0

这个变量可以在onContext函数中访问,但是我想将提取的ajax json结果赋值给网格的商店对象。 网格是像gride对象:{商店:“一些JSON的”}像例如访问dojojs中的这个变量时出错

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.storethis.grid.params.store财产

回答

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中的所有可用内容,但是其他任何内容都无法访问。

+0

通过这样做我得到了这个错误“k.query不是一个函数” – vaibhav

0

您必须使用dojo/_base/lang->hitch函数来设置您的函数将被执行的范围。

在你的情况this.grid.params... referes给当时(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(); 
     }) 
    ); 
};