Unable to access global variable entire one controller in sencha

牧云@^-^@ 提交于 2019-12-08 02:43:10

问题


I have been trying to access global value inside one controller, but could not access it. i have been following this How to define global variable in sencha but could not set and access global values.

in Controller

config: {
    successMessage:100,
    control: {
      'submitnewword': {
         activate: 'onActivate',
         itemtap: 'onItemTap',
         ConfirmTestCommand:'Confirm'
        },
    .......
   },
  onSearchKeyUp: function(searchField) {
    success: function (response) {
       this.setSuccessMessage(1);
   }
   else {
     this.setSuccessMessage(0);
   }   
}

and access it

Confirm: function () {
   console.log("Confirm----- Caling on Controller");
   var testing=this.getSuccessMessage();
   console.log("Confirm----- value--"+testing);         
},

I dont know, what is the wrong with my code.

I am getting this on console:

Uncaught TypeError: Object [object global] has no method 'setSuccessMessage'


回答1:


The problem is about scope and you can solve it by:

In your controller init method

init : function() {
   me = this;
}

Now access the methods using

   me.setSuccessMessage(1)

   me.getSuccessMessage();



回答2:


I think you ran onto the scope visibility issue... I'm not sure where an exception is emitted, from onSearchKeyUp() or from Confirm(). But guess inside it this points not to the controller instance. To help you more you need share more info (sources)




回答3:


The success function inside onSearchKeyUp has a different scope when it is called. So, inside it, this is not your controller where the successMessage (and of course, its getter and setter) is defined.

A solution is to "bind" the success function to the controller scope using:

success: Ext.bind(function (response) {
       this.setSuccessMessage(1);
}, this);


来源:https://stackoverflow.com/questions/18459564/unable-to-access-global-variable-entire-one-controller-in-sencha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!