Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJS: Component VS Element VS other

I've been working with ExtJS for a good few months now, but still don't have a perfectly clear map of what's going on behind the scenes there. I'm just talking about the building blocks of the framework and their most basic functionality.

  • Ext.Component
  • Ext.Element
  • DOM.Element
  • DOM.Node (?)
  • CompositeElement (?)
  • Whatever else (?)

For each of the abovementioned I would like to know:

  1. How to select: by ID, by class, via parent, find (OR children OR query OR select? WTF), siblings, ComponentQuery, DOM-query, CSS-query, etc..

  2. How to manipulate in the tree: create, append, prepend, insert after this sibling, move to that parent, remove, destroy, etc..

  3. How to manipulate on the screen: show, hide, fade, slide, move up, down, change size, etc..

  4. How to identify related to each other: find DOM.Element knowing its Ext.Component, find Ext.Component knowing its DOM.Element, etc..

  5. What is the dependency between them: what happens to the DOM.Element if its Ext.Component is hidden / destroyed / changed / moved, what happens to the Ext.Component if its Ext.Element is hidden / destroyed / changed / moved, etc.

I'm looking for a methodical and logically clear layout of how each is supposed to be used and is expected to behave. I am also hoping that the described functionality can be grouped in corresponding categories, e.g. would be nice to see complement traversing methods .up() and .down() next to each other, rather than alphabetically pages apart. Of course links and examples (which the official documentation lacks so badly) are also welcome!

like image 380
Geo Avatar asked Sep 11 '13 15:09

Geo


People also ask

What is the difference between an ext element and an ext component?

Answer: An Ext. element encapsulates a DOM element, adding simple DOM manipulation facilities, normalizing for browser differences, whereas an Ext. component is the base class for all objects in Ext, such as controls, panels and stores.

What is component in Ext JS?

The Ext JS Component class plays a major role in every UI widget in the framework. Knowing how each UI widget is going to behave introduces stability and predictability into the framework. The Component model also supports direct instantiation of classes, or deferred instantiation, known as XTypes.

What is the use of xType in Ext JS?

xtype is an easy way to create Components without using the full class name. This is especially useful when creating a Ext. Container that contains child Components. An xtype is simply a shorthand way of specifying a Component - for example you can use xtype: 'panel' instead of typing out Ext.

What is autoEl in Ext JS?

autoEL. The autoEl config option lets you customize the Element that will encapsulate the Component. By default, all Components are wrapped in a div tag, but autoEl provides a way to specify some other tag. In addition, you can provide HTML that will be rendered inside the Element.


2 Answers

You can find out a whole lot about the building blocks of ExtJS (known as Ext Core) from the manual for this: http://docs.sencha.com/core/manual/. I will try to add some knowledge and highlight some key points, but you should definitely read through that for a very informative overview.

Ext.Component
The building block for the OOP paradigm within ExtJS. Essentially, this is an Object that contains inherited functionality to serve as the basis for a specialized component that will be transformed by the framework into DOM elements that are shown as HTML.

The Sencha documentation is excellent for this. Here are a couple good places to start: http://docs.sencha.com/extjs/4.2.1/#!/guide/layouts_and_containers http://docs.sencha.com/extjs/4.2.1/#!/guide/components

Ext.Element vs DOM Element
As an JavaScript develop knows, a DOM element is just a JS object that represents a tag in the document's HTML. Essentially, Ext.Element is a wrapper object for a DOM element that allows for ExtJS to manipulate the object. Any DOM element on the page can be wrapped as an Ext.Element to allow for this additional functionality.

For example, take this HTML tag:

<div id="myDiv">My content</div>

You can access this using

var el = document.getElementById('myDiv')

and use the basic, built-in JavaScript DOM functionality on el. You could also access this using

var el = Ext.get('myDiv')

and have a whole additional set of functionality available to apply to that element using the ExtJS library

The Sencha docs are also excellent for this. See all the available functionality for Ext.Element here: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.dom.Element

Misc
You can get an Ext.Element from a component using the getEl() method:

var myComponent = Ext.create('Ext.Component', { html: 'my component' });
var el = myComponent.getEl();

You would rarely need to go the other way, to determine a component from a DOM element. There isn't much of a use case there unless you are really hacking something. A major reason for using a framework like ExtJS is to prevent needing to do something like this; if should develop entirely within the JavaScript, you should be able to avoid having a reference to a DOM element where you need to get its containing ExtJS component.

Niklas answered pretty well about how to select components and elements. The only things I would really add is that you can use up() and down() to select relative to a component. In this way, you should use itemId on components rather than the global identifier id (using id can cause difficult-to-debug errors if you are reusing components with the same ID).

Also, to add to Niklas's answer about showing/hiding components, the framework does indeed add some CSS to the component's element, depending on what the hideMode for the component is. Learn more about that property here: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.AbstractComponent-cfg-hideMode

An excellent way to learn more is to look through all of the examples that come packaged with the framework. Open the examples in your browser, then look through the code to find out how things are done. You will find it way easier to learn this way, rather than reading it on paper or a website. Nothing beats experience when it comes to learning something new.

like image 102
kevhender Avatar answered Nov 07 '22 14:11

kevhender


How to select: by ID, by class, via parent, find (OR children OR query OR select? WTF), siblings, ComponentQuery, DOM-query, CSS-query, etc..

Ext.ComponentQuery.query("*")                   // get all
Ext.ComponentQuery.query("button")              // all buttons
Ext.ComponentQuery.query("#myid")               // all controls / components myid
Ext.ComponentQuery.query("#myid", rootelement)  // all controls / components myid with rootelement
Ext.ComponentQuery.query("#myid,button")        // all buttons or controls / components myid

How to manipulate in the tree: create, append, prepend, insert after this sibling, move to that parent, remove, destroy, etc..

Adding button to a View:

Ext.ComponentQuery.query("#viewId")[0].add(new Ext.Button({ text: 'test'}));

There is also insert, remove and so on depending on the control you are querying.

How to manipulate on the screen: show, hide, fade, slide, move up, down, change size, etc..

Ext.ComponentQuery.query("button").forEach(function(button){ button.hide(); })      // hide all buttons

There is also show, disable, enable and so on depending on the control you are querying.

How to identify related to each other: find DOM.Element knowing its Ext.Component, find Ext.Component knowing its DOM.Element, etc..

Finding Ext.Component knowing its Dom.Element is pretty easy, you just take the ID from the DOM element and use Ext.ComponentQuery.query("#id"). There is also Ext.select('#id') for getting the object from an ID.

With the element property you can get the DOM:

var dom = Ext.ComponentQuery.query("button")[0].element.dom // Getting the DOM from the first button
var dom2 = component.element.dom                            // does also work as long as component is a valid sencha touch component

What is the dependency between them: what happens to the DOM.Element if its Ext.Component is hidden / destroyed / changed / moved, what happens to the Ext.Component if its Ext.Element is hidden / destroyed / changed / moved, etc.

I think, I'm not sure, that if you call .hide for instance there will be some CSS applied to the DOM for example: display: none. Internally they can use some framework like jQuery for that or the old school version document.getElementById('id').css and so one. If you call .show, it may change to display: block or whatever type it was before(this could be saved in the Sencha Touch class).

I don't know what happens if the DOM element gets destroyed. Probably the element too and then the garbage collector has some work to do.

If there are any more questions / something was unclear or not enough, don't hesitate to ask.

like image 35
Niklas Avatar answered Nov 07 '22 15:11

Niklas