2011-12-21 11 views
2

以下のシナリオの範囲を理解しようとしています。 searchTermsを呼び出す場合、scope:thisの下のthisは、パネル自体ではなくsearchTerms関数を指します。私が他の例から見たものとは異なるようです。私は何のミスをしたのか分かりますか?extjsボタンスコープ

function searchTerms(){ 
     var searchGrid = new Ext.grid.GridPanel({ 

     }); 

     var searchPanel = new Ext.form.FormPanel({ 
      region: 'south', 
      height:150, 
      items:[ 
      { 
       xtype: 'textfield', 
       fieldLabel: 'Keywords', 
      },{ 
       xtype: 'textfield', 
       fieldLabel: 'Label', 
      },{ 
       xtype: 'datefield', 
       fieldLabel: 'Valid till' 
      },new Ext.Button({ 
       text: 'crawl', 
       scope: this, 
       handler: function(b,e){ 
        Ext.Ajax.request({^M 
         url: '/discovery/tsearch',^M 
         params: {^M 
          keywords: this.items[0].getValue(), 
          label: this.items[1].getValue(), 
          valid: this.items[2].getValue(), 
         }, 
        }); 
       } 
      }),], 
     }); 

     var regionPanel = new Ext.Panel({ 
      title: 'search', 
      layout: 'border', 
      items: [searchPanel, searchGrid] 
     }); 

    return regionPanel; 
} 

答えて

5

私はあなたがこれを行うためのものだと思う:私のバージョンは動作しませんなぜ

function searchTerms(){ 
    var searchGrid = new Ext.grid.GridPanel({ 

    }); 

    var searchPanel = new Ext.form.FormPanel({ 
     region: 'south', 
     height:150, 
     items:[ 
     { 
      xtype: 'textfield', 
      fieldLabel: 'Keywords', 
     },{ 
      xtype: 'textfield', 
      fieldLabel: 'Label', 
     },{ 
      xtype: 'datefield', 
      fieldLabel: 'Valid till' 
     }], 
    }); 
    searchPanel.add(new Ext.Button({ 
      text: 'crawl', 
      scope: searchPanel, 
      handler: function(b,e){ 
       Ext.Ajax.request({ 
        url: '/discovery/tsearch', 
        params: { 
         keywords: this.items[0].getValue(), 
         label: this.items[1].getValue(), 
         valid: this.items[2].getValue(), 
        }, 
       }); 
      } 
     }) 
    ); 
    var regionPanel = new Ext.Panel({ 
     title: 'search', 
     layout: 'border', 
     items: [searchPanel, searchGrid] 
    }); 

return regionPanel; 
} 
+0

は、私が知っているかもしれませんか? – goh

+1

これはスコープの問題です。「this」は、searchPanelではなくボタンを作成するときにsearchTermオブジェクトになります。イベントが来るときにコードが最初に実行されるときにスコアを考える必要があります。 – RageZ

+1

違いは、ボタンはsearchPanelに*追加*され、スコープはsearchPanelに設定されます。スコープをsearchPanelに設定できるようにするには、最初にsearchPanelを定義する必要があります。これはsearchPanel内に* inline *ボタンを作成すると不可能です。それは意味をなしますか? – Chau