为什么不调用我的事件处理程序

时间:2023-01-02 23:59:20

I´m using ExtJs4.1, here is my example. The "calendarioChange" is not called in this way:

我正在使用ExtJs4.1,这是我的例子。不以这种方式调用“calendarioChange”:

Ext.define('App.view.atendimento.Agenda', {
  extend: 'Ext.window.Window', 
  maximized: true,
  bodyPadding: 4,
  constrain: true,
  layout: 'hbox',
  items:[
    {
        xtype: 'container',
        width: 300,
        layout: 'vbox',
        items: [
            {
                xtype: 'datepicker',
                handler: this.calendarioChange
            }
        ]
    }
  ],
  calendarioChange: function(picker, date){
    alert('changed');
  }
});

but in thsi way works:

但是以这种方式工作:

 xtype: 'datepicker',
     handler: function(picker, date){
                   alert('changed');
              }

What I´m missing in the first case?

我在第一种情况下遗漏了什么?

Thanks.

谢谢。

2 个解决方案

#1


1  

the problem is you haven't taken in account the scope of your handle. Every time you nest a {} constructor you change the 'this' pointer. In your case:

问题是你没有考虑到你的处理范围。每次嵌套{}构造函数时,都会更改“this”指针。在你的情况下:

this.calendarioChange

Can not work because 'this' is pointing to the datepicker not the Window. You could solve it by adding a function in the event that locate the window and then call to the appropiate method:

无法工作,因为'this'指向的是datepicker而不是Window。你可以通过在找到窗口的事件中添加一个函数然后调用适当的方法来解决它:

items: [
            {
                xtype: 'datepicker',
                handler: function(picker, date) {
                    var window = this.up('window');
                    window.calendarioChange(picker, date);
                }
            }
...

#2


0  

Because the method you define as an event handler is not in this scope. For example you can declare the method outside Ext.define() call and then refer it just by name.

因为您定义为事件处理程序的方法不在此范围内。例如,您可以在Ext.define()调用之外声明该方法,然后仅按名称引用它。

#1


1  

the problem is you haven't taken in account the scope of your handle. Every time you nest a {} constructor you change the 'this' pointer. In your case:

问题是你没有考虑到你的处理范围。每次嵌套{}构造函数时,都会更改“this”指针。在你的情况下:

this.calendarioChange

Can not work because 'this' is pointing to the datepicker not the Window. You could solve it by adding a function in the event that locate the window and then call to the appropiate method:

无法工作,因为'this'指向的是datepicker而不是Window。你可以通过在找到窗口的事件中添加一个函数然后调用适当的方法来解决它:

items: [
            {
                xtype: 'datepicker',
                handler: function(picker, date) {
                    var window = this.up('window');
                    window.calendarioChange(picker, date);
                }
            }
...

#2


0  

Because the method you define as an event handler is not in this scope. For example you can declare the method outside Ext.define() call and then refer it just by name.

因为您定义为事件处理程序的方法不在此范围内。例如,您可以在Ext.define()调用之外声明该方法,然后仅按名称引用它。