2013-07-09 45 views
3

大家好我是全新的extjs和新的JavaScript。我嘗試着。我想要做的就是創建一個帶有各種屬性的面板表單的類,我希望能夠在需要調用myclass.makepanel時創建複製面板表單。我爲什麼要這麼做?我只是學習不同的東西,並逐漸熟悉ExtJS的,我不是想完成什麼特別,這是我到目前爲止的代碼extjs創建使面板形式的類

Ext.define('ryan', { 

    constructor: function() { 

     Ext.create('Ext.form.Panel', { 
      bodyStyle: { 
       "background-color": "green" 
      }, 
      name: 'mypanel', 
      title: 'Animal sanctuary, one animal per location ', 
      width: 300, 
      bodyPadding: 10, 
      test: 'mycat', 
      style: 'background-color: #Fdd;', 
      renderTo: Ext.getBody(), 

      items: [{ 
       id: 'button1', 
       xtype: 'button', 
       text: 'click the button', 
       handler: function() { 
        alert('(<^_^>)') 
       } 
      }, { 
       id: 'wildAnimal', 
       xtype: 'textfield', 
       fieldLabel: 'animal:', 
       name: 'myanimal' 
      }, { //end text field 
       id: 'myCombo', 
       xtype: 'combo', 
       fieldLabel: 'choose your animal', 
       store: animals, 
       queryMode: 'local', 
       displayField: 'name', 
       listeners: { 
        'change': function (field, selectedValue) { 
         Ext.getCmp('wildAnimal').setValue(selectedValue); 
        } 
       } 
      } // end combo 

        ], //end items 



     }); // end .create 
    } // end ffunc 
}); //end ryan 

var animals = Ext.create('Ext.data.Store', { 
    fields: ['id', 'name'], 
    data: [{ 
     "id": 'mycat', 
     "name": "mycat" 
    }, { 
     'id': 'mydog', 
     "name": "mydog" 
    }, { 
     'id': 'sbBarGirls', 
     "name": "BarGirls-when-drunk" 
    }] 
}); //end animals.create // up to here no problems 

Ext.onReady(ryan.constructor()); // can't actually display the panel. 

我道歉格式不直接從我的文字編輯器複製。所以如果我的代碼很難閱讀,我可以再次發佈。我可以將所有內容放在.onReady中,而不是嘗試定義'ryan',但我不想這樣做,因爲我希望只要調用該類就能製作克隆表單。

+0

這個類是沒有意義的,應該延長'Ext.form.Panel'和'initComponent' –

回答

2

你定義一個類,但從來沒有從它創建一個對象:

Ext.onReady(function(){ 
    var a = new ryan(); 
}); 

而且你不必顯式調用類的構造方法。

或者與Ext.create

Ext.onReady(function(){ 
    var a = Ext.create('ryan'); 
}); 
+0

配置它。當我這樣做,我創建多個對象,var a和var b ...表單真的搞砸了......我需要爲每個表單指定頁面上的位置嗎?這是因爲我用我的偵聽器使用ext.getcmp嗎? –

+0

你不能使用對象的id-id應該是明確的。這意味着你必須從你的表單面板項目中刪除id屬性。改用'itemId'。例如:'itemId:'wildAnymal'',然後你可以調用它:'this.up('form')。down('#wildAnimal')。setValue(selectedValue);'在你的監聽器中 –

+0

這裏你的更新代碼:http://jsfiddle.net/KcdwW/ –