2013-05-03 100 views
0

我創建了一個自定義組件,因爲它的keyup事件上有一個文本字段,我需要過濾商店,但我沒有得到事件生成中的任何變量,但在創建對象時,我正在獲取對象。 下面是代碼 - :Extjs-在創建自定義類時,自定義類中的組件不訪問自定義類變量

WildCardWindow = Ext.extend(Ext.Window, { 
         width : 300, 
         height : 265, 
         resizable:true, 
         closeAction:'hide', 
         title:'WildCard Selection Window', 
         autoScroll:true, 
         iconCls:'icon-wildcard', 
         bodyStyle:'background-color:#FFFFFF', 
         //@cfg{Array} data-The array of fields/items to show in the window 
         data: null, 
         store:null, 
         /** 
         * @property 
         * @type String 
         * The message displayed when mouse over on an uncommitted field 
         */ 
         uncommittMsg : '<b>Warning!</b> This field has been newly added in    
             the form designer. ' + 'It <i>can</i> be used now, 
             but you should be sure to save the uncommitted 
             changes ' + 'in the open form designer window.', 
         defaultIconCls : '', 
         initComponent : function(){ 
            this.createStore(this.data); 
            this.items = this.createDataView(); 
            WildCardWindow.superclass.initComponent.call(this); 
         }, 
         createDataView: function(){ 
            this.dataView = new Ext.DataView({ 
                store: this.store, 
                autoWidth:true, 
                tpl: this.createTpl(), 
                autoHeight:true, 
                singleSelect : true, 
                overClass:'icon-view-over', 
                selectedClass:'icon-view-selected', 
                itemSelector:'.icon-dataview-item', 
                style:'cursor:pointer' 
             }); 
             this.textField = new Ext.form.TextField({ 
              fieldLabel: 'To', 
              tabTip:'Start typing to filter by field name', 
              name: 'f_to', 
              enableKeyEvents :true, 
              listeners: { 
               keyup: function() {          
         this.store.filter('name',this.textField.getValue(),true,false); 
         //Here I am not getting this.store and this.textField ??? 
              }} 
             }); 
             return [this.dataView,this.textField] 
         }, 
         createStore: function(data){ 
           this.store = new Ext.data.JsonStore({ 
              data:data, 
              autoDestroy:true, 
              fields:[ 
               {name: 'id'}, 
               {name: 'name'}, 
               {name: 'fieldclass'}, 
               {name: 'type'}, 
               {name: 'options'}, 
               {name: 'isMultiMember',type:'boolean'}, 
               {name: 'isUnCommitted',type:'boolean'} 
              ] 
            }); 
            return this.store; 
          }, 
          listeners:{ 
           close: function(){ 
             this.store.filter('name','',true,false); 
           } 
          } 
}) 

在文本框的KEYUP我沒有得到this.store和this.textfield? 任何建議或我錯了。 請儘快回覆

+0

請使用縮進來格式化您的代碼,以使其更具可讀性。 – 2013-05-03 04:58:20

回答

0

因爲在調用該函數時會丟失範圍。

你可以做兩件事情:

使用綁定的功能來複制範圍:

http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.Function-method-bind

我認爲這也適用,是一種更優雅的解決方案:

var me = this; 
this.textField = new Ext.form.TextField({ 
    fieldLabel: 'To', 
    tabTip:'Start typing to filter by field name', 
    name: 'f_to', 
    enableKeyEvents :true, 
    listeners: { 
    keyup: function() {          
     me.store.filter('name',this.getValue(),true,false); 
    }} 
}); 
+0

感謝彼得,工作得很好。 – user2345587 2013-05-03 12:24:03