I have been using extjs’s
Ext.getCmp('xtypeid').getValue();
Ext.getCmp('xtypeId').setValue('');
to get a value or set a value… I just found out that. this is a bad programming in terms of OOP… but my question is how do you go about getting value or setting values bunch of textbox. It would be great if someone could show me a sample program doing just this.
They are required here cause it happens more than just setting the value. Please keep in mind that JavaScript is a Prototype-based programming language so you cannot compare it with a OOP article that you may have read for Java or C#.
What is bad is the use of
getCmp()you should avoid that if possible and completely useComponentQueryor encapsulatingrefs. And if possible avoid settingidby yourself if not strictly required. With component query you can query for any custom or common property. For example theitemIdis in addition supported by ExtJS and for buttons I use a custom property calledactionYou should not concern about methods provided by the framework, do it when you extend or create classes and follow the patterns there.
For clarification:
Ext.getCmp()is not the bad thing, it is really fast, bad is fact that most people set theidby them self so that they can usegetCmp()which can lead to duplicated id’s and that is a problem.Update
As mentioned in the comment the Ext.ComponentQuery is quite mighty and flexible. I will not cover all in this view lines, just one basic example.
Lets make a simple environment. We have a class called
Customwhich we have extended from Panel. Custom has a Toolbar in the topdockedItemsand a Toolbar in the bottomdockedItems.Now we are creating instances from our new Class and how it come, we have two instances on screen at the same time.
Now you want to subscribe yourself to button-events within a centralized class but you will need to identify each button for that. You might have seen that I defined some properties like
itemId(which is really known by ExtJS and can also be used two query on just one component. See the API) andaction. These are names that I use but you might use any here. And now we build our unique path to query with. We will do this for the ‘foo’ button in the bottom Toolbar of the first panel:First we know that we are looking for a panel. You might say hey we are looking for
customcause that’s our class. But that doesn’t matter cause theComponentQuerylooks if the class is extended frompanel. But this is the first point where we can reduce our first result set and we will do this choosing the xtype of our extend classcustom.We now get all instances of the class Custom or any class that extend it. Now that are too much, we need one. So we add a filter property.
Now we have the first Panel. Let’s do the same for the toolbar and the button
And that’s pretty much is it.
See the JSFiddle