2017-09-20 3 views
0

でこの変数にアクセスしている間、この変数はonContext機能でアクセス可能ですが、私は、グリッドのストアオブジェクトにフェッチされたAjaxのJSONの結果を割り当てます。 {店舗:「一部-JSON」} グリッドgride等を目的とする例のようエラー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()機能。私はthis.grid.storethis.grid.params.store財産

答えて

0

に私のAJAX JSONの結果を代入したいので、あなたが「これはいつもあなたにその文脈でプロパティまたはオブジェクトを与える

self.grid.set("store",data); //self contains reference to the entire widget because in init function, you have assigned 'this' to 'self' variable. 

dijit.byId("srv_grid").set("store",data); 

または

を使用することができます。 xhr.getの内部では、xhrの一部として利用可能なすべてのものにアクセスできますが、それ以外のものはアクセスできません。

+0

このエラーが発生しました。「k.queryは関数ではありません」 – vaibhav

0

関数を実行するスコープを設定するには、dojo/_base/lang->hitch関数を使用する必要があります。重要なの後

:あなたのケースでは

その後、(XHR)へthis.grid.params... referesので、それはあなたが以下のようにモジュールの範囲内で機能

だから、あなたが実行する必要が未定義のエラーを返します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(); 
     }) 
    ); 
}; 
関連する問題