【问题标题】:error while accessing this variable in dojojs在 dojojs 中访问此变量时出错
【发布时间】:2017-09-20 11:52:54
【问题描述】:

这个变量可以在 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 属性,在 @987654325 中无法访问 this.grid 属性@.Properties 在xhr.get() 中更改。我想在 xhr.get() 函数中访问 this 的所有属性。因为我想将我的 ajax json 结果分配给this.grid.storethis.grid.params.store 属性

【问题讨论】:

    标签: javascript dojo dojox.grid.datagrid aps


    【解决方案1】:

    你可以使用

    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.
    

    'this' 始终为您提供该上下文中的属性或对象。因此,在您的 xhr.get 中,您可以访问作为 xhr 一部分可用的所有内容,但无法访问它之外的任何内容。

    【讨论】:

    • 这样做我得到了这个错误“k.query is not a function”
    【解决方案2】:

    你必须使用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();
          })
       );
    };
    

    【讨论】:

      猜你喜欢
      • 2017-12-24
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多