9
votes

I am new to ExtJS. I started to program a little form. And I got completely confused about the use of Ext.create and the new operator.

So here is the code:

I wanted to program a form. I found a small example on one of the sencha pages. It creates a form like this:

var descAndSystem = new Ext.form.Panel ({
    region: 'center',
    layout: 'vbox',
    margins: '5 5 5 5',
    xtype: 'form',
    title: 'Some title',
    id: 'descAndSystem',
    width: '800', 
    items: [
       { xtype: 'textarea',
     fieldLabel: 'Provide a description',
     name: 'rightdescription',
       },
       {
      xtype: 'combobox',
      fieldLabel: 'Choose System',
      store: systems,
      queryMode: 'local',
      displayField: 'name',
      valueField: 'name',
      name: 'system'
       }
    ]
});

then I used descAndSystem as component in a viewport:

Ext.create('Ext.container.Viewport', {
    layout: 'border',
    id: 'wizardcontainer',
    items: [
        descAndSystem,
        {
            region: 'south', 
            layout: 'hbox',
            margins: '5 5 5 5',
            items: [
               { xtype: 'button', text: '<< Back', handler: onNext },
               { xtype: 'button', text: 'Next >>', handler: onNext },
               { xtype: 'button', text: 'Cancel', align: 'right', handler: function () { alert ('Abgebrochen geklickt.'); } }
            ]
        }
   ]
});

After a lot of trial and error I found that I can access the values of my form by the following code:

Ext.getCmp ('descAndSystem').getForm ().findField ('rightdescription').getValue ()

in contrast to what one of the books I bought said the following code did NOT work:

Ext.getCmp ('rightdescription').getValue ()

But my real problem is that I would expect that

Ext.create ('Ext.form.Panel', { .... });

is the same as

new Ext.form.Panel ( {...});

But when I do the latter the Chrome interpreter says:

Uncaught TypeError: Cannot read property 'Panel' of undefined'

Again, after a lot of trial and error, the following worked:

new Ext.Panel ( {...});

Not only that I couldn't find any reference to an object of that name in the documentation, also the line

Ext.getCmp ('descAndSystem').getForm ().findField ('rightdescription').getValue ()

now yields an error:

Uncaught TypeError: Object [object Object] has no method 'getForm'

In addition, I was trying to replace descAndSystem by another form by DOM manipulation, there are various replace methods in the documentation. None of them worked, I always got the error message "has no method 'replace'". I have the strong suspicion that I got something wrong fundamentally. Any hints? I am using ExtJS 4 and Chromium 17.0.963.56 on Ubuntu 10.10 64-bit.

4
You must be doing something wrong. Ext.create('className', {}) would produce the same result as new className({}).pllee

4 Answers

9
votes

The major difference is that Ext.create('Ext.form.Panel') will automatically download the appropriate javascript file if the Ext.form.Panel class does not exist. The vanilla new operator can not do this - it has no idea what a Ext.form.Panel might be or where the definition might be found.

The Cannot read property 'Panel' of undefined' error indicates that not only is Ext.form.Panel not defined, neither is the parent Ext.form namespace object.

0
votes

Change the line

name: 'rightdescription',

to

id: 'rightdescription',

and you can access your textarea directly using the:

var yourTextArea = Ext.getCmp ('rightdescription');
var yourTextAreaContent = yourTextArea.getValue();
0
votes
var descAndSystem = Ext.create('Ext.form.Panel', {...})

Here is the documentation for Ext.create() http://docs.sencha.com/ext-js/4-0/#!/api/Ext-method-create

As for your other issues...

  1. When you say new Ext.Panel({...}) you are no longer creating a form panel so the getForm() method will not be available.
  2. As @NTom showed, give any component an id and you will be able to access it using Ext.getCmp. http://docs.sencha.com/ext-js/4-0/#!/api/Ext-method-getCmp
0
votes

I think I found the problem. I copied the example from he sencha page:

http://www.sencha.com/learn/getting-started-with-ext-js-4

the index.html includes ext-debug.js. When I include ext-all-debug.js, then the new operator starts working :-)

Thanks for your help. Greets Kai