error while accessing this variable in dojojs

62 Views Asked by At

this variable is accessible in the onContext function but I want to assign fetched ajax json result to grid's store object. grid is the object like gride:{store:"some-Json"} like example

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 is the first method that calls first, then onContext method call.. this word is available in that function and this variable has this.grid property that this.grid property is not accessible in xhr.get().Properties of this are changed in xhr.get(). I want to access all the properties of this inside xhr.get() function. Because I want to assign my ajax json result to this.grid.store and this.grid.params.store property

2

There are 2 best solutions below

0
On

You have to use the dojo/_base/lang -> hitch function , to set the scope in wich your function will be executed .

In your case the this.grid.params... referes to the then(xhr) so it'll return an undefined error

So you have to execute you function in the scope of the module as below :

after important 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();
      })
   );
};
1
On

you can use

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

or

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

'this' always gives you the property or objects in that context. So in your case inside xhr.get, you are able to access everything that available as part of xhr but anything outside of it is not accessible.