<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Saki's Blog &#187; Know-how</title>
	<atom:link href="http://blog.extjs.eu/category/know-how/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.extjs.eu</link>
	<description>For good of all productive developers</description>
	<lastBuildDate>Mon, 16 Aug 2010 23:12:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Factory Functions in Ext Extensions (Abstract Classes)</title>
		<link>http://blog.extjs.eu/know-how/factory-functions-in-ext-extensions/</link>
		<comments>http://blog.extjs.eu/know-how/factory-functions-in-ext-extensions/#comments</comments>
		<pubDate>Sat, 07 Aug 2010 13:37:15 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[Know-how]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=546</guid>
		<description><![CDATA[<br/>I have recently run across one of the the Jay Garcia&#8217;s excellent screencasts Abstract classes with Ext JS. (Thank you Jay for your effort of educating Ext JS community.)
I&#8217;ve been consulting in a company at that time &#8211; developers of the client, after seeing the screencast, immediately asked me: &#8220;So what should we use? The [...]]]></description>
			<content:encoded><![CDATA[<br/><p>I have recently run across one of the the <a href="http://tdg-i.com/">Jay Garcia&#8217;s</a> excellent screencasts <a href="http://tdg-i.com/364/abstract-classes-with-ext-js">Abstract classes with Ext JS</a>. (Thank you Jay for your effort of educating Ext JS community.)</p>
<p>I&#8217;ve been consulting in a company at that time &#8211; developers of the client, after seeing the screencast, immediately asked me: &#8220;So what should we use? The <a href="http://blog.extjs.eu/know-how/writing-a-big-application-in-ext/">&#8216;xtype&#8217; style</a> or Abstract classes?&#8221;</p>
<p>The curt answer would be: &#8220;Use whatever you prefer.&#8221; or &#8220;Use both&#8221;. </p>
<p>However, decisions based on mere &#8220;preference&#8221; or on &#8220;I use it because it is available&#8221; are often not fully rational and can lead to troubles as the application grows. Thus, let&#8217;s take a deeper look to see if there are any pitfalls and to find how can we get most of both methods. </p>
<p><b><i>Note:</i></b> Before you continue, read <a href="http://blog.extjs.eu/know-how/writing-a-big-application-in-ext/">&#8220;Writing a big application in Ext&#8221;</a>,  watch the J<a href="http://tdg-i.com/364/abstract-classes-with-ext-js">ay&#8217;s screencast</a> and understand both, otherwise, the following text won&#8217;t make any sense to you.</p>
<h1>Abstract ExtJS Class</h1>
<p>Abstract classes are not to be used directly but they serve more like templates that specify methods and properties to be implemented in classes derived from them. They are very useful in object oriented programming because they force developers to implement all mandatory methods with same signature (signature = arguments the method or function accepts) and return values so there is a greater chance to develop a bug free code.</p>
<p>However, in ExtJS (and JavaScript that is scripting language where source is not compiled but directly interpreted), there is no mechanism to warn developer &#8220;you haven&#8217;t implemented method XY&#8221; or &#8220;your implementation has wrong signature&#8221; so we need to take care ourselves.</p>
<p>Nevertheless, the idea of abstract classes is useful also in JavaScript/ExtJS environment because:</p>
<ol>
<li>
logic and configuration common to all expected descendants can be put in the abstract base class that eliminates the code duplication, speeds up development and debugging, and increases future code maintainability greatly
</li>
<li>
abstract classes tell us which methods to implement
</li>
<li>
it increases the human readability of the code (Remember, we always try to write the code to be readable and understandable by us and others in the future.)
</li>
</ol>
<h1>The Basic Idea</h1>
<p>The basic idea Jay presents in his great screencast is to extend an Ext Component, FormPanel for example, and add stub methods to it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">ns</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MyApp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
MyApp.<span style="color: #660066;">AbstractFormPanel</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">form</span>.<span style="color: #660066;">FormPanel</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
     submitUrl<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span>
    <span style="color: #339933;">,</span>initComponent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
             items<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildItems</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">,</span>buttons<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildButtons</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        MyApp.<span style="color: #660066;">AbstractFormPanel</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">initComponent</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function initComponent</span>
&nbsp;
    <span style="color: #339933;">,</span>buildItems<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildItems</span>
&nbsp;
    <span style="color: #339933;">,</span>buildButtons<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildButtons</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// eo extend</span></pre></div></div>

<p>The empty &#8220;buildXxx&#8221; methods will be implemented in the abstract class extension.</p>
<h1>Tweaking the Abstract Class example</h1>
<p>While perfect for the educational purposes, we need to tweak this code if want to use it in the production quality application. I shall walk you through recommended improvements.</p>
<h2>initialConfig</h2>
<p>Ext.Component takes the config object passed to its constructor and saves it in the object variable <code>initialConfig</code> when it instantiates. Although items and buttons in combination with the FormPanel in the above example do not rely on it, so the example runs as expected, <code>initialConfig</code> is used on the Ext.Component level so it may be used by any of Component descendants. </p>
<p>Let&#8217;s take care of it and modify <code>initComponent</code> as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #339933;">,</span>initComponent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// create config object</span>
        <span style="color: #003366; font-weight: bold;">var</span> config <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// build config properties</span>
        Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>config<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
             items<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildItems</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">,</span>buttons<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildButtons</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// apply config</span>
        Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initialConfig</span><span style="color: #339933;">,</span> config<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// call parent</span>
        MyApp.<span style="color: #660066;">AbstractFormPanel</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">initComponent</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function initComponent</span></pre></div></div>

<h2>Return Values</h2>
<p>Abstract methods in the example return empty arrays. That is no problem for items or buttons but if we want to have also <code>buildTbar</code> or <code>buildBbar</code> factory functions that return empty arrays [] then top and bottom toolbars are always created, but empty, if methods are not implemented in derived classes. (Empty toolbars are quite ugly when rendered.)</p>
<p>Therefore, always return undefined from the abstract methods. For now, the class could look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">ns</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MyApp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
MyApp.<span style="color: #660066;">AbstractFormPanel</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">form</span>.<span style="color: #660066;">FormPanel</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
     submitUrl<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span>
    <span style="color: #339933;">,</span>initComponent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// create config object</span>
        <span style="color: #003366; font-weight: bold;">var</span> config <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// build config properties</span>
        Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>config<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
             items<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildItems</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">,</span>buttons<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildButtons</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">,</span>tbar<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildTbar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">,</span>bbar<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildBbar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// apply config</span>
        Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initialConfig</span><span style="color: #339933;">,</span> config<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// call parent</span>
        MyApp.<span style="color: #660066;">AbstractFormPanel</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">initComponent</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function initComponent</span>
&nbsp;
    <span style="color: #339933;">,</span>buildItems<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildItems</span>
&nbsp;
    <span style="color: #339933;">,</span>buildButtons<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildButtons</span>
&nbsp;
    <span style="color: #339933;">,</span>buildTbar<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildTbar</span>
&nbsp;
    <span style="color: #339933;">,</span>buildBbar<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildBbar</span></pre></div></div>

<h2>Passing <code>config</code> to Abstract Factory Methods</h2>
<p>If we pass <code>config</code> object to abstract methods then we can apply the created items, buttons, toolbar items directly to it. That has no great benefit in itself, however, if we create a sequence or interceptor of these methods we can use the config object directly.</p>
<p>So now we have:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">ns</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MyApp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
MyApp.<span style="color: #660066;">AbstractFormPanel</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">form</span>.<span style="color: #660066;">FormPanel</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
     submitUrl<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span>
    <span style="color: #339933;">,</span>initComponent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// create config object</span>
        <span style="color: #003366; font-weight: bold;">var</span> config <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// build config properties</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildItems</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildButtons</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildTbar</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildBbar</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// apply config</span>
        Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initialConfig</span><span style="color: #339933;">,</span> config<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// call parent</span>
        MyApp.<span style="color: #660066;">AbstractFormPanel</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">initComponent</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function initComponent</span>
&nbsp;
    <span style="color: #339933;">,</span>buildItems<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">items</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildItems</span>
&nbsp;
    <span style="color: #339933;">,</span>buildButtons<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">buttons</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildButtons</span>
&nbsp;
    <span style="color: #339933;">,</span>buildTbar<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">tbar</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildTbar</span>
&nbsp;
    <span style="color: #339933;">,</span>buildBbar<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">bbar</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildBbar</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// eo extend</span></pre></div></div>

<h1>The Final Touch</h1>
<p>If you look in the <code>initComponent</code> now you see that we call a series of &#8220;build&#8221; functions in it. Let&#8217;s move it into another factory method:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">ns</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MyApp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
MyApp.<span style="color: #660066;">AbstractFormPanel</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">form</span>.<span style="color: #660066;">FormPanel</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
     submitUrl<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span>
    <span style="color: #339933;">,</span>initComponent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// create config object</span>
        <span style="color: #003366; font-weight: bold;">var</span> config <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// build config</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildConfig</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// apply config</span>
        Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initialConfig</span><span style="color: #339933;">,</span> config<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// call parent</span>
        MyApp.<span style="color: #660066;">AbstractFormPanel</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">initComponent</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function initComponent</span>
&nbsp;
    <span style="color: #339933;">,</span>buildConfig<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildItems</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildButtons</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildTbar</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildBbar</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildConfig</span>
&nbsp;
    <span style="color: #339933;">,</span>buildItems<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">items</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildItems</span>
&nbsp;
    <span style="color: #339933;">,</span>buildButtons<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">buttons</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildButtons</span>
&nbsp;
    <span style="color: #339933;">,</span>buildTbar<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">tbar</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildTbar</span>
&nbsp;
    <span style="color: #339933;">,</span>buildBbar<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">bbar</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildBbar</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// eo extend</span></pre></div></div>

<p>The above gives you the flexibility of implementing or overriding of the whole <code>buildConfig</code> function or individual items, buttons, bars functions. It also takes care of <code>initialConfig</code> problem.</p>
<p>It&#8217;s a kind of a &#8220;pattern&#8221; and I will publish it in my &#8220;file patterns&#8221; series on this blog with more code comments.</p>
<h1>Example</h1>
<p>If we build upon Jay&#8217;s example, using the pattern above, we get:</p>
<p><code>AbstractFormPanel.js</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">ns</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MyApp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
MyApp.<span style="color: #660066;">AbstractFormPanel</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">form</span>.<span style="color: #660066;">FormPanel</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
     defaultType<span style="color: #339933;">:</span><span style="color: #3366CC;">'textfield'</span>
    <span style="color: #339933;">,</span>frame<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span>
    <span style="color: #339933;">,</span>width<span style="color: #339933;">:</span><span style="color: #CC0000;">300</span>
    <span style="color: #339933;">,</span>height<span style="color: #339933;">:</span><span style="color: #CC0000;">200</span>
    <span style="color: #339933;">,</span>labelWidth<span style="color: #339933;">:</span><span style="color: #CC0000;">75</span>
    <span style="color: #339933;">,</span>submitUrl<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span>
    <span style="color: #339933;">,</span>submitT<span style="color: #339933;">:</span><span style="color: #3366CC;">'Submit'</span>
    <span style="color: #339933;">,</span>cancelT<span style="color: #339933;">:</span><span style="color: #3366CC;">'Cancel'</span>
    <span style="color: #339933;">,</span>initComponent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// create config object</span>
        <span style="color: #003366; font-weight: bold;">var</span> config <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            defaults<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>anchor<span style="color: #339933;">:</span><span style="color: #3366CC;">'-10'</span><span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// build config</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildConfig</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// apply config</span>
        Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initialConfig</span><span style="color: #339933;">,</span> config<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// call parent</span>
        MyApp.<span style="color: #660066;">AbstractFormPanel</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">initComponent</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function initComponent</span>
&nbsp;
    <span style="color: #339933;">,</span>buildConfig<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildItems</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildButtons</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildTbar</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildBbar</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildConfig</span>
&nbsp;
    <span style="color: #339933;">,</span>buildItems<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">items</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildItems</span>
&nbsp;
    <span style="color: #339933;">,</span>buildButtons<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">buttons</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
             text<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">submitT</span>
            <span style="color: #339933;">,</span>scope<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>
            <span style="color: #339933;">,</span>handler<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onSubmit</span>
            <span style="color: #339933;">,</span>iconCls<span style="color: #339933;">:</span><span style="color: #3366CC;">'icon-disk'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
             text<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cancelT</span>
            <span style="color: #339933;">,</span>scope<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>
            <span style="color: #339933;">,</span>handler<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onCancel</span>
            <span style="color: #339933;">,</span>iconCls<span style="color: #339933;">:</span><span style="color: #3366CC;">'icon-undo'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildButtons</span>
&nbsp;
    <span style="color: #339933;">,</span>buildTbar<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">tbar</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildTbar</span>
&nbsp;
    <span style="color: #339933;">,</span>buildBbar<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">bbar</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildBbar</span>
&nbsp;
    <span style="color: #339933;">,</span>onSubmit<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Ext.<span style="color: #660066;">MessageBox</span>.<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Submit'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">submitUrl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function onSubmit</span>
&nbsp;
    <span style="color: #339933;">,</span>onCancel<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">el</span>.<span style="color: #660066;">mask</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'This form is canceled'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function onCancel</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// eo extend</span></pre></div></div>

<p>Mention please that I moved hard-wired button texts to class properties. The reason is that this way we can easily localize (translate) these texts to another languages. I didn&#8217;t take care of messages of handlers though. You will do in your localizable applications, won&#8217;t you?</p>
<p><code>AddressFormPanel.js</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">ns</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MyApp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
MyApp.<span style="color: #660066;">AddressFormPanel</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>MyApp.<span style="color: #660066;">AbstractFormPanel</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
     title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Edit address data'</span>
    <span style="color: #339933;">,</span>submitUrl<span style="color: #339933;">:</span><span style="color: #3366CC;">'addressAction.asp'</span>
    <span style="color: #339933;">,</span>buildItems<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">items</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
             <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'address1'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'Address 1'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
             <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'address2'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'Address 2'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
             <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'city'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'city'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
             xtype<span style="color: #339933;">:</span><span style="color: #3366CC;">'combo'</span>
            <span style="color: #339933;">,</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'state'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'State'</span>
            <span style="color: #339933;">,</span>store<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'MD'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'VA'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'DC'</span><span style="color: #009900;">&#93;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
             xtype<span style="color: #339933;">:</span><span style="color: #3366CC;">'numberfield'</span>
            <span style="color: #339933;">,</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'zip'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'Zip Code'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildItems</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// eof</span></pre></div></div>

<p><code>NameFormPanel.js</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">ns</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MyApp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
MyApp.<span style="color: #660066;">NameFormPanel</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>MyApp.<span style="color: #660066;">AbstractFormPanel</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
     title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Edit name data'</span>
    <span style="color: #339933;">,</span>submitUrl<span style="color: #339933;">:</span><span style="color: #3366CC;">'nameAction.asp'</span>
    <span style="color: #339933;">,</span>okT<span style="color: #339933;">:</span><span style="color: #3366CC;">'OK'</span>
&nbsp;
    <span style="color: #339933;">,</span>buildItems<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">items</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
             <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'firstName'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'First Name'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
             <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'lastName'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'Last Name'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
             <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'middleName'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'Middle Name'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
             xtype<span style="color: #339933;">:</span><span style="color: #3366CC;">'datefield'</span>
            <span style="color: #339933;">,</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'dob'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'DOB'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildItems</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//Extension</span>
    <span style="color: #339933;">,</span>buildButtons<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// let parent build buttons first</span>
        MyApp.<span style="color: #660066;">NameFormPanel</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">buildButtons</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// tweak the submit button</span>
        config.<span style="color: #660066;">buttons</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">text</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">okT</span><span style="color: #339933;">;</span>
        config.<span style="color: #660066;">buttons</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">handler</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onOkBtn</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildButtons</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//Override</span>
    <span style="color: #339933;">,</span>onOkBtn<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'OK btn pressed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function onOkBtn</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// eo extend</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// eof</span></pre></div></div>

<p>With this setup, we can even create instance of AbstractForm panel directly passing some/all build functions inline:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> nameForm <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MyApp.<span style="color: #660066;">AbstractFormPanel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
     title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Name Form Panel configured inline'</span>
    <span style="color: #339933;">,</span>width<span style="color: #339933;">:</span><span style="color: #CC0000;">300</span>
    <span style="color: #339933;">,</span>height<span style="color: #339933;">:</span><span style="color: #CC0000;">200</span>
    <span style="color: #339933;">,</span>renderTo<span style="color: #339933;">:</span>Ext.<span style="color: #660066;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">,</span>buildItems<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">items</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
             <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'firstName'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'First Name'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
             <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'lastName'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'Last Name'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
             <span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'middleName'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'Middle Name'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
             xtype<span style="color: #339933;">:</span><span style="color: #3366CC;">'datefield'</span>
            <span style="color: #339933;">,</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'dob'</span>
            <span style="color: #339933;">,</span>fieldLabel<span style="color: #339933;">:</span><span style="color: #3366CC;">'DOB'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildItems</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><i><b>Note:</b></i> Although the above works, it violates the rule of not instantiating an abstract class directly. It&#8217;s up to you if you will do it or not.</p>
<h1>Combining with &#8220;xtypes&#8221;</h1>
<p>Now, do not succumb to the temptation of &#8220;putting everything&#8221; in factory functions. Abstract classes are just a programming style, not an universal solvent.</p>
<p>Imagine, you have your own carefully crafted Submit and Cancel buttons so if you would &#8220;put everything&#8221; in the factory functions you would need to copy buttons configurations many times because also toolbars, grids, data views, etc can contain them.</p>
<p>Create extensions (xtypes) for those buttons instead. The following illustrates the approach:</p>
<p><code>SubmitButton.js</code></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">ns</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MyApp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
MyApp.<span style="color: #660066;">SubmitButton</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">Button</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
     text<span style="color: #339933;">:</span><span style="color: #3366CC;">'Submit'</span>
    <span style="color: #339933;">,</span>iconCls<span style="color: #339933;">:</span><span style="color: #3366CC;">'icon-disk'</span>
    <span style="color: #339933;">,</span>initComponent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        MyApp.<span style="color: #660066;">SubmitButton</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">initComponent</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function initComponent</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// eo extend</span>
&nbsp;
Ext.<span style="color: #660066;">reg</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'submitbutton'</span><span style="color: #339933;">,</span> MyApp.<span style="color: #660066;">SubmitButton</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// eof</span></pre></div></div>

<p><code>CancelButton.js</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">ns</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MyApp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
MyApp.<span style="color: #660066;">CancelButton</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">Button</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
     text<span style="color: #339933;">:</span><span style="color: #3366CC;">'Cancel'</span>
    <span style="color: #339933;">,</span>iconCls<span style="color: #339933;">:</span><span style="color: #3366CC;">'icon-undo'</span>
    <span style="color: #339933;">,</span>initComponent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        MyApp.<span style="color: #660066;">CancelButton</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">initComponent</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function initComponent</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// eo extend</span>
&nbsp;
Ext.<span style="color: #660066;">reg</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cancelbutton'</span><span style="color: #339933;">,</span> MyApp.<span style="color: #660066;">CancelButton</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// eof</span></pre></div></div>

<p>and <code>AbstractFormPanel.js</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">ns</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MyApp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
MyApp.<span style="color: #660066;">AbstractFormPanel</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">form</span>.<span style="color: #660066;">FormPanel</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
     defaultType<span style="color: #339933;">:</span><span style="color: #3366CC;">'textfield'</span>
    <span style="color: #339933;">,</span>frame<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span>
    <span style="color: #339933;">,</span>width<span style="color: #339933;">:</span><span style="color: #CC0000;">300</span>
    <span style="color: #339933;">,</span>height<span style="color: #339933;">:</span><span style="color: #CC0000;">200</span>
    <span style="color: #339933;">,</span>labelWidth<span style="color: #339933;">:</span><span style="color: #CC0000;">75</span>
    <span style="color: #339933;">,</span>submitUrl<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span>
    <span style="color: #339933;">,</span>initComponent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// create config object</span>
        <span style="color: #003366; font-weight: bold;">var</span> config <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            defaults<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>anchor<span style="color: #339933;">:</span><span style="color: #3366CC;">'-10'</span><span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// build config</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildConfig</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// apply config</span>
        Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initialConfig</span><span style="color: #339933;">,</span> config<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// call parent</span>
        MyApp.<span style="color: #660066;">AbstractFormPanel</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">initComponent</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function initComponent</span>
&nbsp;
    <span style="color: #339933;">,</span>buildConfig<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildItems</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildButtons</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildTbar</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">buildBbar</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildConfig</span>
&nbsp;
    <span style="color: #339933;">,</span>buildItems<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">items</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildItems</span>
&nbsp;
    <span style="color: #339933;">,</span>buildButtons<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">buttons</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
             xtype<span style="color: #339933;">:</span><span style="color: #3366CC;">'submitbutton'</span>
            <span style="color: #339933;">,</span>scope<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>
            <span style="color: #339933;">,</span>handler<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onSubmit</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
             xtype<span style="color: #339933;">:</span><span style="color: #3366CC;">'cancelbutton'</span>
            <span style="color: #339933;">,</span>scope<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>
            <span style="color: #339933;">,</span>handler<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onCancel</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildButtons</span>
&nbsp;
    <span style="color: #339933;">,</span>buildTbar<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">tbar</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildTbar</span>
&nbsp;
    <span style="color: #339933;">,</span>buildBbar<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        config.<span style="color: #660066;">bbar</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function buildBbar</span>
&nbsp;
    <span style="color: #339933;">,</span>onSubmit<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Ext.<span style="color: #660066;">MessageBox</span>.<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Submit'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">submitUrl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function onSubmit</span>
&nbsp;
    <span style="color: #339933;">,</span>onCancel<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">el</span>.<span style="color: #660066;">mask</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'This form is canceled'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function onCancel</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// eo extend</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// eof</span></pre></div></div>

<p><b><i>Note:</i></b> The above buttons are far too simple to be extended in the real world but you get the point, right?</p>
<h1>Conclusion</h1>
<p>Abstract classes, as Jay Garcia defines and presents them, are a very useful ExtJS programming technique and if you use them in the information provided in this post your code will be clean, flexible and maintainable. That is what all we developers want.</p>
<p>Happy coding!</p>
<p>Further reading:</p>
<ol>
<li>
<a href="http://blog.extjs.eu/know-how/writing-a-big-application-in-ext/">Writing a Big Application in Ext</a>
</li>
<li><a href="http://tdg-i.com/364/abstract-classes-with-ext-js">Abstract classes with Ext JS</a>
</li>
</ol>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="js02@aariadne.com" /><input type="hidden" name="item_number" value="Blog Donation" /><input type="hidden" name="currency_code" value="EUR" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/know-how/factory-functions-in-ext-extensions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>What the hell is mon and mun?</title>
		<link>http://blog.extjs.eu/know-how/what-the-hell-is-mon-and-mun/</link>
		<comments>http://blog.extjs.eu/know-how/what-the-hell-is-mon-and-mun/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 00:31:51 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[Know-how]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=446</guid>
		<description><![CDATA[<br/>I&#8217;ve run across the following code in some of Ext 3.x examples:

this.mon&#40;toolsUl, 'click', this.onClick, this&#41;;

Looks like event handler installation but what it really does anyway?
So I delved into the code and remembered ExtJS Conference and now it is clear to me.
Imagine you create a component and then you need to install an event handler, for [...]]]></description>
			<content:encoded><![CDATA[<br/><p>I&#8217;ve run across the following code in some of Ext 3.x examples:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">mon</span><span style="color: #009900;">&#40;</span>toolsUl<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onClick</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Looks like event handler installation but what it really does anyway?</p>
<p>So I delved into the code and remembered ExtJS Conference and now it is clear to me.</p>
<p>Imagine you create a component and then you need to install an event handler, for example, on its body. Something like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> handler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'You clicked my body'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Ext.<span style="color: #660066;">Panel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
     renderTo<span style="color: #339933;">:</span>Ext.<span style="color: #660066;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">,</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Panel with a listener on the body'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
p.<span style="color: #660066;">body</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Easy enough, we&#8217;ve done something like this many times. But, if the panel is ever to be destroyed we need to remove this listener ourselves as Ext knows nothing about it.</p>
<p> Something like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Ext.<span style="color: #660066;">Panel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
     renderTo<span style="color: #339933;">:</span>Ext.<span style="color: #660066;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">,</span>title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Panel with a listener on the body'</span>
    <span style="color: #339933;">,</span>beforeDestroy<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">body</span>.<span style="color: #660066;">un</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
If we install our listener as inline function, such as:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">p.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'You clicked my body'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</p>
<p>then it is impossible to remove this listener selectively, you would need to use</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">p.<span style="color: #660066;">body</span>.<span style="color: #660066;">removeAllListeners</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>to remove <i>all</i> body listeners.</p>
<p>Now, if we use <code>mon</code> (m stands for &#8220;managed&#8221;) this way</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">p.<span style="color: #660066;">mon</span><span style="color: #009900;">&#40;</span>p.<span style="color: #660066;">body</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>then the listener is automatically removed by Ext on panel destroy. Nice, isn&#8217;t it? Saves a lot of our work and also handles our laziness or forgetfulness about destroying and cleanups of components.</p>
<p>
Mind the first argument, that is the element to install the listener to. It can, but not necessarily have to be within the component. Also remember that when you destroy <code>p</code> Ext only removes listener from the element so if the element is outside of <code>p</code> you need to remove it yourself if it is not necessary anymore.
</p>
<p>Should you ever need to remove the listener installed by <code>mon</code> you use <code>mun</code> with same arguments. You cannot selectively remove inline listeners.</p>
<p><code>mon</code> and <code>mun</code> are defined in Component so you can use them from Component down the descendant classes chain.</p>
<p>
<span style="color:#c00000;font-size:14px;font-weight:bold">Warning! <code>mon</code> and <code>mun</code> are not documented (yet?) so there is still (a little) risk that they will change or will be removed.</span>
</p>
<p>&nbsp;</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="js02@aariadne.com" /><input type="hidden" name="item_number" value="Blog Donation" /><input type="hidden" name="currency_code" value="EUR" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/know-how/what-the-hell-is-mon-and-mun/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Mastering Ext.Direct, Part 3</title>
		<link>http://blog.extjs.eu/know-how/mastering-ext-direct-part-3/</link>
		<comments>http://blog.extjs.eu/know-how/mastering-ext-direct-part-3/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 12:20:37 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[Know-how]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=421</guid>
		<description><![CDATA[<br/>]]></description>
			<content:encoded><![CDATA[<br/><h4><a href="http://blog.extjs.eu/know-how/mastering-ext-direct-part-2"><<< Mastering Ext.Direct, Part 2</a></h4>
<h1>Processing responses</h1>
<p>As I&#8217;ve already said, you cannot use the return value of an API function call as result is not know at the moment of the call return. It is never stressed enough that
<p><b>Ext.Direct calls are ASYNCHRONOUS</b>.</p>
<p>Therefore, all Ext.Direct created API functions, for example <code>Example.Car.go()</code> take one extra argument in addition to arguments you have specified in server-side class definition. That argument is <b>callback function</b>.
</p>
<p>
In our example, <code>Example.Car.go()</code> takes one argument, <code>speed</code> per server side method definition, however, it takes two in fact: <code>Example.Car.go(speed, callback)</code>
</p>
<p>
Let&#8217;s call it with a callback to test it. Type in the Firebug console:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">go</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">80</span><span style="color: #339933;">,</span> console.<span style="color: #660066;">info</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><code>console.info</code> is a function that prints its arguments in a human readable form. We use it as the callback so we see:</p>
<ul>
<li>that it really gets called when the response arrives</li>
<li>which arguments Ext.Direct passes to the callback</li>
</ul>
<p>
As you can see, the callback is called with two arguments:</p>
<ul>
<li><code>response</code> &#8211; it is that what is returned by the server-side <code>Car.go()</code> method. It can be string (as in our example), boolean, object, array or void (nothing).</li>
<li><code>e</code> &#8211; it is <a target="_blank" href="http://www.extjs.com/deploy/dev/docs/?class=Ext.Direct&#038;member=eventTypes">Ext.Direct.Event</a> object or, being more specific, Ext.Direct.RemotingEvent.
</ul>
</p>
<p>
The event <code>e</code> has some useful properties and methods, most important of them are:</p>
<ul>
<li><code>status</code> is <code>true</code> if the call was successful, <code>false</code> if the call failed</li>
<li><code>result</code> contains same value as the first argument &#8211; it is server response data</li>
<li><code>type</code> is <code>rpc</code> in our example but it also can be <code>exception</code> or others
<li><code>action</code> is the class name</li>
<li><code>method</code> is name of the method called</li>
<li><code>getTransaction()</code> is method to retrieve the transaction used in this server round trip. You would use it if you want more data on the transaction.
</ul>
</p>
<h1>Processing exceptions</h1>
<p>Let&#8217;s call go method again:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">go</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> console.<span style="color: #660066;">info</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The callback is called as before but the arguments differ:</p>
<ul>
<li><code>response</code> is <code>undefined</code> &#8211; no data from server</li>
<li><code>e</code> has type <code>exception</code> and has property <code>message</code> that contains error message text</li>
</ul>
<h1>An example application</h1>
<p>That is almost all we need to know to start using Ext.Direct in an application. I&#8217;ve written a simple one so you have something to play with.</p>
<p>First, create file <code>example.js</code> with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * Mastering Ext.Direct example script
 *
 * @author     Ing. Jozef Sakalos
 * @copyright  (c) 2009, by Ing. Jozef Sakalos
 * @date       16. September 2009
 * @version    1.0
 * @revision   $Id$
 */</span>
&nbsp;
Ext.<span style="color: #660066;">BLANK_IMAGE_URL</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'ext/resources/images/default/s.gif'</span><span style="color: #339933;">;</span>
&nbsp;
Ext.<span style="color: #660066;">onReady</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">/**
     * This function is called when Ext.Direct request
     * response arrives from the server.
     *
     * @param {String/Object/Array/Null} response 
     * That what is returned by server method
     * @param {Ext.Direct.RemotingEvent/Ext.Direct.ExceptionEvent} e 
     */</span>
    <span style="color: #003366; font-weight: bold;">var</span> callback <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #339933;">,</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// uncomment if you want to inspect arguments in Firebug Console</span>
<span style="color: #006600; font-style: italic;">//        console.log(response, e);</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">status</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;b&gt;Success&lt;/b&gt;'</span>
        <span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// success handling - e.status is success flag: </span>
        <span style="color: #006600; font-style: italic;">// true is success, false is failure</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span> <span style="color: #339933;">===</span> e.<span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// response argument is same as e.result</span>
            text <span style="color: #339933;">=</span> response<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// failure handling</span>
        <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066;">status</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;b&gt;&lt;i&gt;Failure&lt;/i&gt;&lt;/b&gt;'</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// in the case of an exception, we don't have response but message</span>
            text <span style="color: #339933;">=</span> e.<span style="color: #660066;">message</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// grab the center body</span>
        <span style="color: #003366; font-weight: bold;">var</span> body <span style="color: #339933;">=</span> win.<span style="color: #660066;">items</span>.<span style="color: #660066;">itemAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">body</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// display the response</span>
        body.<span style="color: #660066;">createChild</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
             tag<span style="color: #339933;">:</span><span style="color: #3366CC;">'div'</span>
            <span style="color: #339933;">,</span>cls<span style="color: #339933;">:</span><span style="color: #3366CC;">'response'</span>
            <span style="color: #339933;">,</span>html<span style="color: #339933;">:</span><span style="color: #000066;">status</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">': '</span> <span style="color: #339933;">+</span> text
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// scroll down</span>
        body.<span style="color: #660066;">scrollTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'top'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100000</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #006600; font-style: italic;">// create Ext.Direct test window</span>
    <span style="color: #003366; font-weight: bold;">var</span> win <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Ext.<span style="color: #660066;">Window</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
         title<span style="color: #339933;">:</span><span style="color: #3366CC;">'Mastering Ext.Direct by Saki'</span>
        <span style="color: #339933;">,</span>width<span style="color: #339933;">:</span><span style="color: #CC0000;">600</span>
        <span style="color: #339933;">,</span>height<span style="color: #339933;">:</span><span style="color: #CC0000;">400</span>
        <span style="color: #339933;">,</span>closable<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span>
        <span style="color: #339933;">,</span>layout<span style="color: #339933;">:</span><span style="color: #3366CC;">'border'</span>
        <span style="color: #339933;">,</span>border<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span>
        <span style="color: #339933;">,</span>items<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// west region with buttons</span>
             region<span style="color: #339933;">:</span><span style="color: #3366CC;">'west'</span>
            <span style="color: #339933;">,</span>width<span style="color: #339933;">:</span><span style="color: #CC0000;">160</span>
            <span style="color: #339933;">,</span>minSize<span style="color: #339933;">:</span><span style="color: #CC0000;">160</span>
            <span style="color: #339933;">,</span>split<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span>
            <span style="color: #339933;">,</span>defaults<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>minWidth<span style="color: #339933;">:</span><span style="color: #CC0000;">120</span><span style="color: #009900;">&#125;</span>
            <span style="color: #339933;">,</span>layout<span style="color: #339933;">:</span><span style="color: #3366CC;">'table'</span>
            <span style="color: #339933;">,</span>bodyStyle<span style="color: #339933;">:</span><span style="color: #3366CC;">'padding:20px'</span>
            <span style="color: #339933;">,</span>layoutConfig<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>columns<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> tableAttrs<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>style<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>width<span style="color: #339933;">:</span><span style="color: #3366CC;">'100%'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span>
            <span style="color: #339933;">,</span>items<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
                 xtype<span style="color: #339933;">:</span><span style="color: #3366CC;">'button'</span>
                <span style="color: #339933;">,</span>text<span style="color: #339933;">:</span><span style="color: #3366CC;">'Car.start()'</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// a delegate is needed if we want to pass arguments</span>
                <span style="color: #339933;">,</span>handler<span style="color: #339933;">:</span>Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">start</span>.<span style="color: #660066;">createDelegate</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>callback<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
                 xtype<span style="color: #339933;">:</span><span style="color: #3366CC;">'button'</span>
                <span style="color: #339933;">,</span>text<span style="color: #339933;">:</span><span style="color: #3366CC;">'Car.go(80)'</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// a delegate is needed if we want to pass arguments</span>
                <span style="color: #339933;">,</span>handler<span style="color: #339933;">:</span>Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">go</span>.<span style="color: #660066;">createDelegate</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">80</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
                 xtype<span style="color: #339933;">:</span><span style="color: #3366CC;">'button'</span>
                <span style="color: #339933;">,</span>text<span style="color: #339933;">:</span><span style="color: #3366CC;">'Car.go(250)'</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// a delegate is needed if we want to pass arguments</span>
                <span style="color: #339933;">,</span>handler<span style="color: #339933;">:</span>Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">go</span>.<span style="color: #660066;">createDelegate</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">250</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
                 xtype<span style="color: #339933;">:</span><span style="color: #3366CC;">'button'</span>
                <span style="color: #339933;">,</span>text<span style="color: #339933;">:</span><span style="color: #3366CC;">'Car.stop()'</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// a delegate is needed if we want to pass arguments</span>
                <span style="color: #339933;">,</span>handler<span style="color: #339933;">:</span>Example.<span style="color: #660066;">Car</span>.<span style="color: #000066;">stop</span>.<span style="color: #660066;">createDelegate</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>callback<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
                 xtype<span style="color: #339933;">:</span><span style="color: #3366CC;">'button'</span>
                <span style="color: #339933;">,</span>text<span style="color: #339933;">:</span><span style="color: #3366CC;">'Send All'</span>
&nbsp;
                <span style="color: #006600; font-style: italic;">// another option is inline function that executes calls</span>
                <span style="color: #339933;">,</span>handler<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
                    <span style="color: #006600; font-style: italic;">// the following calls will be combined in one request</span>
                    Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span>callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">go</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">80</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">go</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">250</span><span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    Example.<span style="color: #660066;">Car</span>.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span>callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// responses are displayed here</span>
             region<span style="color: #339933;">:</span><span style="color: #3366CC;">'center'</span>
            <span style="color: #339933;">,</span>autoScroll<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span>
            <span style="color: #339933;">,</span>tbar<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'-&gt;'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
                text<span style="color: #339933;">:</span><span style="color: #3366CC;">'Clear'</span>
               <span style="color: #339933;">,</span>handler<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>win.<span style="color: #660066;">items</span>.<span style="color: #660066;">itemAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">body</span>.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    win.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// eof</span></pre></div></div>

</p>
<p>
Then tune your <code>index.php</code> so that it reads:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?require_once<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;config.php&quot;</span><span style="color: #66cc66;">&#41;</span>;?&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ext/resources/css/ext-all.css&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ext/adapter/ext/ext-base.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ext/ext-all-debug.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;page-title&quot;</span>&gt;</span>Mastering Ext.Direct by Saki<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
      .x-table-layout-cell {
        height:40px;
        text-align:center;
        vertical-align:middle;
    }
    .x-table-layout-cell table {
        margin:auto
    }
    .response {
        padding:4px 0 4px 20px;
        font-size:13px;
    }
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;api.php&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
      Ext.Direct.addProvider(Example.API);
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;example.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

</p>
<h1>Conclusion</h1>
<p>Good! Play with Ext.Direct and let me know what you would like to have in next parts.<br />
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="js02@aariadne.com" /><input type="hidden" name="item_number" value="Blog Donation" /><input type="hidden" name="currency_code" value="EUR" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/know-how/mastering-ext-direct-part-3/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Mastering Ext.Direct, Part 2</title>
		<link>http://blog.extjs.eu/know-how/mastering-ext-direct-part-2/</link>
		<comments>http://blog.extjs.eu/know-how/mastering-ext-direct-part-2/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 20:50:58 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[Know-how]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=384</guid>
		<description><![CDATA[<br/>add() call and add /** @remotable */ comments to methods
multiple requests and responses are combined in one if they come fast enough

Mastering Ext.Direct, Part 3 >>>
]]></description>
			<content:encoded><![CDATA[<br/><h4><a href="http://blog.extjs.eu/know-how/mastering-ext-direct-part-1"><<< Mastering Ext.Direct, Part 1</a></h4>
<h1>Adding server-side classes</h1>
<p>
After completing part 1, we have the basic setup running. Although Ext.Direct PHP stack example classes are fine for demonstrating the functionality we will need our own PHP classes in the real life. I&#8217;m not going to explain basics of PHP object oriented programming, anyway, there is a couple of things to point out.
</p>
<p>
Let&#8217;s create (useless) class <code>Car</code> from part1. For that, create file <code>classes/Car.php</code> with the following content:
</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// vim: sw=4:ts=4:fdc=4:nospell</span>
    <span style="color: #000000; font-weight: bold;">class</span> Car <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">/**
          * @remotable
          */</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Started&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// eo function start</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/**
          * @remotable
          */</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> go<span style="color: #009900;">&#40;</span><span style="color: #000088;">$speed</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;The speed is <span style="color: #006699; font-weight: bold;">$speed</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// eo function go</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/**
          * @remotable
          */</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Stopped&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// eo function stop</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> repair<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Done&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// eo function repair</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// eo class car</span>
<span style="color: #666666; font-style: italic;">// eof</span></pre></div></div>

<p>Then edit <code>api.php</code> and add <code>Car</code> to <code>$api->add()</code> call so that it reads:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #000088;">$api</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'Echo'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'prefix'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Class_'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'Exception'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'prefix'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Class_'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'Time'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'File'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'Car'</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// ...</span></pre></div></div>

<p>Reload <code>index.php</code> and type in Firebug console:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You should get the following response:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;rpc&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;tid&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;action&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Car&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;method&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;start&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;result&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Started&quot;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now type:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">repair</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Error, yes? Why? Take a deeper look at <code>Car</code> source code. Some methods have comments</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">        <span style="color: #009933; font-style: italic;">/**
          * @remotable
          */</span></pre></div></div>

<p>
but <code>repair</code> does not have it. And that is the single reason why it is not exported to API so it is not known to the client application. You can verify it by typing:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Example.<span style="color: #660066;">API</span>.<span style="color: #660066;">actions</span>.<span style="color: #660066;">Car</span><span style="color: #339933;">;</span></pre></div></div>

<p> in Firebug console.</p>
<p>If you want to know how is it done study <code>ExtDirect_API</code> PHP class in <code>classes/API.php</code> file and <a href="http://php.net/manual/en/class.reflectionclass.php" target="_blank">ReflectionClass</a> documentation. In any case, it makes our life very easy as we only need to:</p>
<ul>
<li>write server side classes</li>
<li>add the above comment to the methods that can be called remotely</li>
</ul>
<p>Can it be simpler?</p>
<h1>Returning values</h1>
<p>Type in Firebug console:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now you see that the return value of the call is <code>undefined</code> despite of the fact that server side method returns string <code>"Started"</code>. This leads us to a very important note:</p>
<p>
<strong>Ext.Direct calls are ASYNCHRONOUS.</strong>
</p>
<p>That means that the code execution does <strong>NOT</strong> wait until the call is finished but it immediately continues. So never try to use the return value, neither assume that you have data available at the line that follows the call.
</p>
<p>
The value returned from the server side method call is wrapped in the request response and it arrives when it arrives. It can take milliseconds or seconds depending on the connection speed, server execution time and other factors.
</p>
<p>I will discuss response processing in the next part</p>
<h1>Exceptions</h1>
<p>Edit <code>go</code> method of our <code>Car</code> class so that it reads:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">        <span style="color: #009933; font-style: italic;">/**
          * @remotable
          */</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> go<span style="color: #009900;">&#40;</span><span style="color: #000088;">$speed</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$speed</span> <span style="color: #339933;">||</span> <span style="color: #cc66cc;">200</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$speed</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Speed must be between 0 and 200&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;The speed is <span style="color: #006699; font-weight: bold;">$speed</span> <span style="color: #006699; font-weight: bold;">$unit</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// eo function go</span></pre></div></div>

<p>and execute:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">go</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You get the following response:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;exception&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;tid&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;message&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Speed must be between than 0 and 200&quot;</span><span style="color: #339933;">,</span>
<span style="color: #3366CC;">&quot;where&quot;</span><span style="color: #339933;">:</span>
<span style="color: #3366CC;">&quot;#0 [internal function]: Car-&gt;go(300)<span style="color: #000099; font-weight: bold;">\n</span>
#1 <span style="color: #000099; font-weight: bold;">\/</span>ddata1<span style="color: #000099; font-weight: bold;">\/</span>devel<span style="color: #000099; font-weight: bold;">\/</span>extjs.eu<span style="color: #000099; font-weight: bold;">\/</span>direct<span style="color: #000099; font-weight: bold;">\/</span>ExtDirect<span style="color: #000099; font-weight: bold;">\/</span>Router.php(176): call_user_func_array(Array, Array)<span style="color: #000099; font-weight: bold;">\n</span>
#2 <span style="color: #000099; font-weight: bold;">\/</span>ddata1<span style="color: #000099; font-weight: bold;">\/</span>devel<span style="color: #000099; font-weight: bold;">\/</span>extjs.eu<span style="color: #000099; font-weight: bold;">\/</span>direct<span style="color: #000099; font-weight: bold;">\/</span>ExtDirect<span style="color: #000099; font-weight: bold;">\/</span>Router.php(62): ExtDirect_Router-&gt;rpc(Object(stdClass))<span style="color: #000099; font-weight: bold;">\n</span>
#3 <span style="color: #000099; font-weight: bold;">\/</span>ddata1<span style="color: #000099; font-weight: bold;">\/</span>devel<span style="color: #000099; font-weight: bold;">\/</span>extjs.eu<span style="color: #000099; font-weight: bold;">\/</span>direct<span style="color: #000099; font-weight: bold;">\/</span>router.php(23): ExtDirect_Router-&gt;dispatch()<span style="color: #000099; font-weight: bold;">\n</span>
#4 {main}&quot;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>
You see that the response type changed from <code>rpc</code> (remote procedure call) to <code>exception</code> and the response contains member <code>message</code> that is the text of the Exception we&#8217;ve thrown at server side. So far so good, however, <code>where</code> member contains debugging data that, while useful for debugging, shouldn&#8217;t be disclosed in a production environment.
</p>
<p>You can freely throw exceptions while debugging your Ext.Direct application but you should consider another method(s) of handling errors for production systems. I will tackle it again later in this series.
</p>
<h1>Combining requests</h1>
<p>Type the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>Example.<span style="color: #660066;">Car</span>.<span style="color: #660066;">go</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">80</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>Example.<span style="color: #660066;">Car</span>.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The request is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;action&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Car&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;method&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;start&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;data&quot;</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;rpc&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;tid&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;action&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Car&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;method&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;go&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;data&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">80</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;rpc&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;tid&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;action&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Car&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;method&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;stop&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;data&quot;</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;rpc&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;tid&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span></pre></div></div>

<p>and the response is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;rpc&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;tid&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;action&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Car&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;method&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;start&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;result&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Started&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;rpc&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;tid&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;action&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Car&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;method&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;go&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;result&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;The speed is 80&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;rpc&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;tid&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;action&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Car&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;method&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;stop&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;result&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Stopped&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span></pre></div></div>

<p>Ext.Direct combines requests that come within a configurable time frame into one to minimize the server round trips count.</p>
<h1>Conclusion</h1>
<p>Now we know a couple of basics:</p>
<ul>
<li>we need to export API from server to client &#8211; Ext.Direct stack does a lot of dirty work for us</li>
<li>we can call server side methods directly at client prefixed with a configurable namespace (Example in our case)</li>
<li>we <strong>cannot</strong> use return values of client calls because calls are <strong>asynchronous</strong></li>
<li>to add classes we need to add them to $api->add() call and add /** @remotable */ comments to methods</li>
<li>multiple requests and responses are combined in one if they come fast enough
</ul>
<h4><a href="http://blog.extjs.eu/know-how/mastering-ext-direct-part-3">Mastering Ext.Direct, Part 3 >>></a></h4>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="js02@aariadne.com" /><input type="hidden" name="item_number" value="Blog Donation" /><input type="hidden" name="currency_code" value="EUR" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/know-how/mastering-ext-direct-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Mastering Ext.Direct, Part 1</title>
		<link>http://blog.extjs.eu/know-how/mastering-ext-direct-part-1/</link>
		<comments>http://blog.extjs.eu/know-how/mastering-ext-direct-part-1/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 18:07:36 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[Know-how]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=320</guid>
		<description><![CDATA[<br/>
Preface
My first idea how to the name this article was &#8220;Ext.Direct for Dummies&#8221; just because I feel as a one as long as Ext.Direct is concerned. I&#8217;ve first heard about it during Ext Conference in April and I think that it is one of the brightest ideas of Ext 3.x release. Nevertheless, I had no [...]]]></description>
			<content:encoded><![CDATA[<br/><p><a name="Preface"></a></p>
<h1>Preface</h1>
<p>My first idea how to the name this article was &#8220;Ext.Direct for Dummies&#8221; just because I feel as a one as long as Ext.Direct is concerned. I&#8217;ve first heard about it during Ext Conference in April and I think that it is one of the brightest ideas of Ext 3.x release. Nevertheless, I had no time do dig into into it and to understand the concepts fully.</p>
<p>Now I&#8217;ve decided to take a journey of discovering what is under hood, how to setup client and server side and how to use Ext.Direct effectively in applications. If you want, I invite you to travel with me.</p>
<p><a name="Who_is_this_article_for"></a></p>
<h1>Who is this article for</h1>
<p>It is for developers who are familiar (at least) with basic javascript and Ext object oriented programming, who are able to setup a web page and have it running from a http server and who can code in a server-side programming language. (I will use PHP in this article so PHP developers will have a slight advantage.)</p>
<p><a name="What_is_Ext_Direct_anyway"></a></p>
<h1>What is Ext.Direct anyway</h1>
<p>Rich Internet Applications (RIA) consist of two parts: client side and server side. Client cannot call server functions directly but sends requests, server processes them calling the appropriate functions and returns results back to client.
</p>
<p>Let&#8217;s say we have a server side class <code>Car</code> that has methods <code>start</code>, <code>go</code> and <code>stop</code>. From the client viewpoint, we need to ask server: Please, <code>start</code> the <code>Car</code>, then <code>go</code> with it and then <code>stop</code> it.
</p>
<p>
Now, imagine that we could directly call</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Car.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Car.<span style="color: #660066;">go</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Car.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and these would call <strong>server</strong> side methods of <strong>server</strong> side class <code>Car</code>. Nice, isn&#8217;t it? You need to remember only one set of class names and their methods, code is neat and less bug prone.
</p>
<p>
And that is what Ext.Direct does. You export list of server side classes and their methods that should be made available for client to call and Ext.Direct takes care of the rest so that you can really use <code>Car.start()</code> in your code.
</p>
<p><a name="What_we_need"></a></p>
<h1>What we need</h1>
<ol>
<li> a working http server we have an access to. It can be installed on the local computer but it must be present. file:///something links will <strong>not</strong> work</li>
<li> a server side (scripting) language enabled in the above server. If you will use PHP, you need 5+ version to take advantage of <a href="http://php.net/manual/en/class.reflectionclass.php" target="_blank">ReflectionClass</a></li>
<li> <a href="http://www.extjs.com/products/extjs/download.php?dl=extjs3">Ext JS 3.0.0</a> library or latest Ext version built from SVN, if you have purchased <a href="http://www.extjs.com/store/extjs/#support-table" target="_blank">Ext Premium Membership</a></li>
<li> <a href="http://www.extjs.com/products/extjs/download.php?dl=extdirectpack">Ext.Direct Pack</a></li>
<li> <a href="http://getfirefox.com" target="_blank">Firefox</a> with <a href="http://getfirebug.com" target="_blank">Firebug</a> installed</li>
<li> If you use PHP, <a href="http://www.firephp.org/" target="_blank">FirePHP</a> is strongly recommended</li>
</ol>
<p><a name="Initial_setup"></a></p>
<h1>Initial setup</h1>
<ol>
<li> create directory <code>direct</code> under your http server document root. Name does not matter in fact but I will use <code>direct</code> as the root for testing in this article.</li>
<li> extract ExtJS into <code>ext</code> subdirectory under <code>direct</code></li>
<li> extract content of php subdirectory from Ext.Direct Pack under <code>direct</code></li>
<li> extract content of FirePHPCore-x.x.x into <code>firephp</code> subdirectory under <code>direct</code></li>
</ol>
<p>At this point, your <code>direct</code> directory listing should read the following:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">+ cache<span style="color: #000000; font-weight: bold;">/</span>
+ classes<span style="color: #000000; font-weight: bold;">/</span>
+ data<span style="color: #000000; font-weight: bold;">/</span>
+ ext<span style="color: #000000; font-weight: bold;">/</span>
+ ExtDirect<span style="color: #000000; font-weight: bold;">/</span>
+ firephp<span style="color: #000000; font-weight: bold;">/</span>
  api.php
  router.php</pre></div></div>

<p><a name="The_first_test"></a></p>
<h1>The first test</h1>
<p>We still need to do some work before we can see it running. First, create <code>config.php</code> file with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #666666; font-style: italic;">// vim: sw=4:ts=4:fdc=4:nospell</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// authentication would come here in the real world</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// switch it to false if you do not want to use FirePHP</span>
<span style="color: #666666; font-style: italic;">// $useFirePHP would be false also for a production system</span>
<span style="color: #000088;">$useFirePHP</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// require FirePHP files</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$useFirePHP</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;firephp/lib/FirePHPCore/FirePHP.class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;firephp/lib/FirePHPCore/fb.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// define empty fb() function so code does not break </span>
<span style="color: #666666; font-style: italic;">// on any forgotten fb() calls later</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> fb<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// eof</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>We also need <code>index.php</code> so we have something to run:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?require_once<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;config.php&quot;</span><span style="color: #66cc66;">&#41;</span>;?&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ext/resources/css/ext-all.css&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ext/adapter/ext/ext-base.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ext/ext-all-debug.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;page-title&quot;</span>&gt;</span>Mastering Ext.Direct by Saki<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;api.php&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
  	Ext.Direct.addProvider(Example.API);
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Ext.Direct Pack comes with example PHP classes (<code>Echo, Exception, File</code> and <code>Time</code>) and with <code>api.php</code> file. You can leave classes untouched for the moment but edit <code>api.php</code> to read the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;config.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Include ExtDirect PHP Helpers</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ExtDirect/API.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ExtDirect/CacheProvider.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// disable caching for development, enable for production</span>
<span style="color: #666666; font-style: italic;">//$cache = new ExtDirect_CacheProvider('cache/api_cache.txt');</span>
<span style="color: #000088;">$api</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ExtDirect_API<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$api</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRouterUrl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'router.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// default</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// disable caching for development, enable for production</span>
<span style="color: #666666; font-style: italic;">//$api-&gt;setCacheProvider($cache);</span>
&nbsp;
<span style="color: #000088;">$api</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Example'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$api</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDescriptor</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Example.API'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$api</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDefaults</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'autoInclude'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'basePath'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'classes'</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$api</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span>
    <span style="color: #666666; font-style: italic;">// these are example classes from Ext.Direct PHP stack</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #666666; font-style: italic;">// real class name is Class_Echo, therefore prefix</span>
        <span style="color: #0000ff;">'Echo'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'prefix'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Class_'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// real class name is Class_Exception, therefore prefix</span>
        <span style="color: #0000ff;">'Exception'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'prefix'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Class_'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'Time'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'File'</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$api</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">output</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ext-direct-state'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$api</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getState</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// eof</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>
So far, so good&#8230; Now you can navigate to <code>http://yourserver/direct/index.php</code> and if everything went right you will see the blank page and no errors in Firebug.
</p>
<p>
Open Firebug console and type the following:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Example.Time.get<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;</pre></div></div>

<p>You can see that request is sent to server:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;action&quot;</span>:<span style="color: #ff0000;">&quot;Time&quot;</span>,<span style="color: #ff0000;">&quot;method&quot;</span>:<span style="color: #ff0000;">&quot;get&quot;</span>,<span style="color: #ff0000;">&quot;data&quot;</span>:null,<span style="color: #ff0000;">&quot;type&quot;</span>:<span style="color: #ff0000;">&quot;rpc&quot;</span>,<span style="color: #ff0000;">&quot;tid&quot;</span>:<span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>and that response comes back:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;type&quot;</span>:<span style="color: #ff0000;">&quot;rpc&quot;</span>,<span style="color: #ff0000;">&quot;tid&quot;</span>:<span style="color: #000000;">2</span>,<span style="color: #ff0000;">&quot;action&quot;</span>:<span style="color: #ff0000;">&quot;Time&quot;</span>,<span style="color: #ff0000;">&quot;method&quot;</span>:<span style="color: #ff0000;">&quot;get&quot;</span>,<span style="color: #ff0000;">&quot;result&quot;</span>:<span style="color: #ff0000;">&quot;09-05-2009 19:46:38&quot;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>You can try other classes and methods:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Example.<span style="color: #660066;">Echo</span>.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Test to be echoed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Example.<span style="color: #660066;">File</span>.<span style="color: #660066;">list</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Example.<span style="color: #660066;">Time</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and you can also put <code>fb($someVariable)</code> statement in various places of php code if you want to know what&#8217;s going on here and there.</p>
<h1>Conclusion</h1>
<p>We have set up very minimum of Ext.Direct server and client side. The main purpose of this part is to get acquainted with basic components of this technology.</p>
<p><h4><a href="http://blog.extjs.eu/know-how/mastering-ext-direct-part-2">Mastering Ext.Direct, Part 2 >>></a></h4>
</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="js02@aariadne.com" /><input type="hidden" name="item_number" value="Blog Donation" /><input type="hidden" name="currency_code" value="EUR" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/know-how/mastering-ext-direct-part-1/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Factory Function File Pattern</title>
		<link>http://blog.extjs.eu/know-how/factory-function-file-pattern/</link>
		<comments>http://blog.extjs.eu/know-how/factory-function-file-pattern/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 13:50:04 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[File Patters]]></category>
		<category><![CDATA[Know-how]]></category>
		<category><![CDATA[extjs]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=266</guid>
		<description><![CDATA[<br/>You know, I&#8217;m not very big fan of factory functions, nevertheless, I&#8217;m aware of the fact that they may be necessary in some situations. Here is the file pattern that works (briefly tested).
Keep each factory function in a separate file name of which should be Namespace.Factory.functionName.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// vim: ts=4:sw=4:nu:fdc=4:nospell
/*global Ext, MyNamespace */
/**
 * @class MyNamespace.Factory
 *
 [...]]]></description>
			<content:encoded><![CDATA[<br/><p>You know, I&#8217;m not very big fan of factory functions, nevertheless, I&#8217;m aware of the fact that they may be necessary in some situations. Here is the file pattern that works (briefly tested).</p>
<p><b>Keep each factory function in a separate file name of which should be <code>Namespace.Factory.functionName.js</code></b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// vim: ts=4:sw=4:nu:fdc=4:nospell</span>
<span style="color: #009966; font-style: italic;">/*global Ext, MyNamespace */</span>
<span style="color: #006600; font-style: italic;">/**
 * @class MyNamespace.Factory
 *
 * A Factory function pattern
 *
 * @author    Ing. Jozef Sakáloš
 * @copyright (c) 2009, by Ing. Jozef Sakáloš
 * @date      17. March 2009
 * @version   0.1
 * @revision  $Id$
 *
 * @license MyNamespace.Factory.myPanel.js is licensed under the terms of
 * the Open Source LGPL 3.0 license. Commercial use is permitted to the extent
 * that the code/component(s) do NOT become part of another Open Source or 
 * Commercially licensed development library or toolkit 
 * without explicit permission.
 * 
 * &lt;p&gt;License details: &lt;a href=&quot;http://www.gnu.org/licenses/lgpl.html&quot;
 * target=&quot;_blank&quot;&gt;http://www.gnu.org/licenses/lgpl.html&lt;/a&gt;&lt;/p&gt;
 */</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// create namespace</span>
Ext.<span style="color: #660066;">ns</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'MyNamespace.Factory'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * @method myPanel
 * @param {Object} config
 * A config object
 * @return {Ext.Panel}
 */</span>
MyNamespace.<span style="color: #660066;">Factory</span>.<span style="color: #660066;">myPanel</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// pre-instantiation code</span>
	<span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// put your defaults here</span>
		<span style="color: #006600; font-style: italic;">// but avoid id, el, contentEl, renderTo, applyTo, or similar</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// eo defaults object</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// create config object</span>
	<span style="color: #003366; font-weight: bold;">var</span> cfg <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> config<span style="color: #339933;">,</span> defaults<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// instantiate</span>
	<span style="color: #003366; font-weight: bold;">var</span> cmp <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Ext.<span style="color: #660066;">Panel</span><span style="color: #009900;">&#40;</span>cfg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// post-instantiation code</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// return the created component</span>
	<span style="color: #000066; font-weight: bold;">return</span> cmp<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function MyNamespace.Factory.myPanel</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// eof</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/know-how/factory-function-file-pattern/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Writing a Big Application in Ext (Part 3)</title>
		<link>http://blog.extjs.eu/know-how/writing-a-big-application-in-ext-part-3/</link>
		<comments>http://blog.extjs.eu/know-how/writing-a-big-application-in-ext-part-3/#comments</comments>
		<pubDate>Sat, 14 Mar 2009 12:53:34 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[Know-how]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[extjs]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=234</guid>
		<description><![CDATA[<br/>Important
If you have not already done so, study Writing a Big Application in Ext (Part 1) and Writing a Big Application in Ext (Part 2)before you read this article. It would be very hard, if not impossible, to understand concepts explained here before you fully understand the first and second part.
Introduction
Helping on the forum and [...]]]></description>
			<content:encoded><![CDATA[<br/><h2 style="margin-top:30px"><em>Important</em></h2>
<p>If you have not already done so, study <a href="http://blog.extjs.eu/know-how/writing-a-big-application-in-ext/" style="font-size:125%;">Writing a Big Application in Ext (Part 1)</a> and <a href="http://blog.extjs.eu/know-how/writing-a-big-application-in-ext-part-2/" style="font-size:125%;">Writing a Big Application in Ext (Part 2)</a>before you read this article. It would be very hard, if not impossible, to understand concepts explained here before you fully understand the first and second part.</p>
<h1>Introduction</h1>
<p>Helping on the <a href="http://extjs.com/forum" target="_blank">forum</a> and reading code of others that failed to extend Ext classes, revealed more errors that users, especially beginners, commonly make. Therefore, I&#8217;ve decided to start this article that will collect these errors and will explain why the errors <i>are</i> errors. I mean it as loosely ended as I may discover more errors and ways of avoiding them so I plan just to add them to <i>this</i> article, not endlessly create parts 4, 5, etc&#8230;</p>
<h1><span style="font-size:12px">&#8230; continued:</span> Most Common Sources of Troubles</h1>
<p>Here we go:</p>
<ol>
<li><a href="#unnecessary-extending">Unnecessary Extending</a></li>
<li><a href="#adding-objects-to-prototype">Adding Objects to Prototype</a></li>
<li><a href="#hard-coding-ids">Hard Coding ids</a></li>
</ol>
<p>&nbsp;</p>
<h2 id="unnecessary-extending">Unnecessary Extending</h2>
<p>The main reasons for extending are:</p>
<ul>
<li>re-usability</li>
<li>adding functionality</li>
<li>combination of them</li>
</ul>
<p>so we extend if we need a re-usable component <b>or</b> we need to add a functionality (new methods) or both. If we are after <b>re-usability</b> the extension can be as simple as:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">MyPortlet <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">Panel</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
     anchor<span style="color: #339933;">:</span><span style="color: #3366CC;">'100%'</span>
    <span style="color: #339933;">,</span>draggable<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span>
    <span style="color: #339933;">,</span>defaultType<span style="color: #339933;">:</span><span style="color: #3366CC;">'mygraph'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You see what happens? We are going to use MyPortlet many times so instead of scatter the above configuration in 10,000 lines application code 100 times, we create this simple extension and we save 297 lines of code. </p>
<p>The other aspect is that if we upgrade our &#8216;mygraph&#8217; to &#8216;mygraph_new&#8217; the only place where to change it is our extension saving us searching out code for all occurrences of &#8216;mygraph&#8217; (100 times) and replacing it with &#8216;mygraph_new&#8217; 100 times.</p>
<p>(Well, 100 is exaggerated, but you get the point, right?)</p>
<p>If we are after <b>adding functionality</b>, which can be also simple, we add some &#8220;logic&#8221;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">MyPanel <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">Panel</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    onRender<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        MyPanel.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">onRender</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Rendered'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Here we add some logic to Panel, it <b>does more</b> that it did before. </p>
<p><b>There is no need to extend in all other cases.</b></p>
<p>&nbsp;</p>
<h2 id="adding-objects-to-prototype">Adding Objects to Prototype</h2>
<p>Run this code first:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ext/resources/css/ext-all.css&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ext/adapter/ext/ext-base.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ext/ext-all-debug.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;page-title&quot;</span>&gt;</span>Extending Error: Object in prototype<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
  Ext.BLANK_IMAGE_URL = 'ext/resources/images/default/s.gif';
  Ext.onReady(function() {
    MyPanel = Ext.extend(Ext.Panel, {
         layout:'fit'
        ,panelConfig: {
            bodyBg:'red'
        }
&nbsp;
        ,initComponent:function() {
            var config = {
                bodyStyle:'background-color:' + this.panelConfig.bodyBg
            }; // eo config object
&nbsp;
            // apply config
            Ext.apply(this, Ext.apply(this.initialConfig, config));
&nbsp;
            MyPanel.superclass.initComponent.apply(this, arguments);
        } // eo function initComponent
&nbsp;
        ,applyBackground:function(color) {
            this.panelConfig.bodyBg = color;
            this.body.applyStyles({'background-color':color});
        } // eo function applyBackground
&nbsp;
    }); // eo extend
&nbsp;
    var p1 = new MyPanel({
         title:'Panel with Blue Background'
        ,renderTo:Ext.getBody()
        ,width:240
        ,height:160
    });
&nbsp;
    p1.applyBackground('blue');
&nbsp;
    var p2 = new MyPanel({
         title:'Panel with Red Background'
        ,renderTo:Ext.getBody()
        ,width:240
        ,height:160
    });
&nbsp;
  });
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>What do we expect? It is written in titles of panels: Top panel (p1) should have <b>blue</b> body background because we set it to it after it is created. And bottom panel (p2) should have <b>red</b> because we just create default <code>MyPanel</code>.</p>
<p><b>But it is blue too!!! Why?</b> The reason is simple: <code>panelConfig</code> is object that is created during <i>class definition</i> and it is added to MyPanel prototype. Objects (arrays too) are accessed by reference so p1 and p2 <b>share the same instance of <code>panelConfig</code></b>. Setting <code>bodyBg</code> property in <code>applyBackground</code> method changes this single instance of <code>panelConfig</code> object. So we create p2 with blue background too.</p>
<p>You see, here it is clearly and immediately visible that something went wrong but making this error can lead to weeks of wasted debugging time in real applications. Imagine you have a store in prototype&#8230;</p>
<p>&nbsp;</p>
<h2 id="hard-coding-ids">Hard Coding ids</h2>
<p>Very simple, but deadly mistake is to set ids in the extension either to the main extension object or on its items, toolbars, buttons, etc. If a hard coded ids are set we cannot create two or more instances of our extension, can we?</p>
<h1>Loose End</h1>
<p>That&#8217;s all for now but if I discover more errors I will add them above. </p>
<p>Stay tuned!</p>
<p><strong><em>Do not forget to read <a href="http://blog.extjs.eu/know-how/writing-a-big-application-in-ext/">Part 1</a> and <a href="http://blog.extjs.eu/know-how/writing-a-big-application-in-ext-part-2/">Part 2</a> of this article. </em></strong></p>
<p>Follow up: <strong><a href="http://blog.extjs.eu/know-how/factory-functions-in-ext-extensions/">Factory Functions in Ext Extensions (Abstract Classes)</a></strong></p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="js02@aariadne.com" /><input type="hidden" name="item_number" value="Blog Donation" /><input type="hidden" name="currency_code" value="EUR" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form>
]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/know-how/writing-a-big-application-in-ext-part-3/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>How to remove all children of a tree node</title>
		<link>http://blog.extjs.eu/know-how/how-to-remove-all-children-of-a-tree-node/</link>
		<comments>http://blog.extjs.eu/know-how/how-to-remove-all-children-of-a-tree-node/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 21:34:29 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[Know-how]]></category>
		<category><![CDATA[extjs]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=221</guid>
		<description><![CDATA[<br/>Suppose we want to remove all children of the node node:

while&#40;node.firstChild&#41; &#123;
    node.removeChild&#40;node.firstChild&#41;;
&#125;

That&#8217;s all there is to it
]]></description>
			<content:encoded><![CDATA[<br/><p>Suppose we want to remove all children of the node <code><b>node</b></code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>node.<span style="color: #660066;">firstChild</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    node.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>node.<span style="color: #660066;">firstChild</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That&#8217;s all there is to it</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/know-how/how-to-remove-all-children-of-a-tree-node/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Writing a Big Application in Ext (Part 2)</title>
		<link>http://blog.extjs.eu/know-how/writing-a-big-application-in-ext-part-2/</link>
		<comments>http://blog.extjs.eu/know-how/writing-a-big-application-in-ext-part-2/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 12:12:55 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[Know-how]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[extjs]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=137</guid>
		<description><![CDATA[<br/>Important
If you have not already done so, study Writing a Big Application in Ext (Part 1) before you read this article. It would be very hard, if not impossible, to understand concepts explained here before you fully understand the first part.
Introduction

It has been almost one year since I have published the first part of this [...]]]></description>
			<content:encoded><![CDATA[<br/><h2 style="margin-top:30px"><em>Important</em></h2>
<p>If you have not already done so, study <a href="http://blog.extjs.eu/know-how/writing-a-big-application-in-ext/" style="font-size:125%;">Writing a Big Application in Ext (Part 1)</a> before you read this article. It would be very hard, if not impossible, to understand concepts explained here before you fully understand the first part.</p>
<h1>Introduction</h1>
<p>
It has been almost one year since I have published the first part of this article. I have been successfully using the concept of pre-configured classes personally to write a really big application (~150 javascript files, ~60,000 lines of code, plus server-side and css). The application is fully modularized, each class in separate file and it has proven that it can be easily developed, managed and debugged.
</p>
<p>
Unfortunately, the same <em>has not been always</em> true for other users, they were hitting various problems and Ext Support Team have had to handle may questions and help requests. These accumulated to the degree where the concept has been called <em>&#8220;absolute abomination&#8221;</em> (read absolutely hated) and it was stated that <em>&#8220;it causes problems&#8221;</em>.
</p>
<p><em>Note: As &#8220;fast cars&#8221; do not cause accidents but &#8220;slow drivers driving fast cars&#8221; do, the concept itself cannot be a cause of the problems but its application can.</em>
</p>
<p>
In any case, there must be some illogic if I can use the concept but others cannot.
</p>
<p>Thus, I have looked deeper in it and I have isolated some pitfalls the users can run into on the course of development. I will also write on &#8220;dos&#8221; and &#8220;don&#8217;ts&#8221; and on when to use a pre-configured class and when not. I will <em>not</em> compare this concept to another application design concepts (factory functions, in-line configuration, on-demand load, or other) because 1) I do not use them and 2) I do not want to turn this article into a Linux versus Windows discussion.
</p>
<p>
It is fully up to you which application design concept you choose. However, if you do choose this one then follow the rules I&#8217;m going to lay down.</p>
<h1>Most Common Sources of Troubles</h1>
<ul>
<li>Dull Copy&amp;Paste</li>
<li>Extending a non-Ext.Component class</li>
<li>Not calling parent methods</li>
<li>initialConfig</li>
<li>listeners</li>
</ul>
<h2 style="margin-top:30px">Dull Copy&amp;Paste</h2>
<p>Do you know such people? They post on a forum:</p>
<blockquote><p>I need to drag from tree to qrid, gimme plz complete codez</p></blockquote>
<p>And if somebody altruistic writes a fragment of &#8220;codez&#8221; for him in a sheer attempt to help the response is going to be:
</p>
<blockquote><p>Your codez don&#8217;t work. Help me plz my manager wants it working</p></blockquote>
<p>
Do you see what happened? A dull &#8220;developer&#8221; <i>ordered</i> a code on the forum, he&#8217;s got some, copied&amp;pasted it to his application without a clue what the code does, maybe hasn&#8217;t even changed url that still points to your server and the result is: <em>it doesn&#8217;t work</em>.
</p>
<p>
Well, this is an extreme (but not so rare as you would think), nevertheless, copying&amp;pasting without understanding of what the copied&amp;pasted code does can lead only to frustrations.
</p>
<p>
I am not against Copy&amp;Paste in general, it can save a lot of time and I also occasionally do it, but <strong>I am against not-understanding or mis-understanding</strong> not only of coding but also of life.
</p>
<p><em>The Rule:</em> <strong>Do Copy&amp;Paste but always with full understanding of what the code does.</strong></p>
<h2 style="margin-top:30px">Extending a non-Ext.Component class</h2>
<p>If an Ext class does not inherit from Ext.Component the <code>initComponent</code> is never called so the code you have written there is never executed. This is fragment from Ext.Component constructor:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Ext.<span style="color: #660066;">ComponentMgr</span>.<span style="color: #660066;">register</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Ext.<span style="color: #660066;">Component</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">constructor</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">baseAction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">baseAction</span>.<span style="color: #660066;">addComponent</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initComponent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
Ext classes that do not inherit from Ext.Component do not have <code>this.initComponent();</code> line in their constructors.
</p>
<p><em>The Rule:</em> <strong>Always check if the Ext class you are going to pre-configure inherits from Ext.Component. You have to use an another approach if it does not.</strong>
</p>
<h2 style="margin-top:30px">Not calling parent methods</h2>
<p>
It happens very often that you do not only <em>add</em> some methods in your extended class but that you <em>modify</em> existing ones. <code>initComponent</code> being the first example. <code>onRender</code>, <code>afterLayout</code> are other (but not only) frequently overriden methods.
</p>
<p>
These methods <em>are already implemented</em> in the class you are extending and its parents so if you forget the line:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// in initComponent override</span>
YourExtension.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">initComponent</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// or in onRender override</span>
YourExtension.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">onRender</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// or in afterLayout override</span>
YourExtension.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">afterLayout</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>your class <em>will not work</em>.
</p>
<p><em>The Rule:</em> <strong>Never forget to call the parent method, unless you exactly know what you are doing.</strong>
</p>
<h2 style="margin-top:30px">initialConfig</h2>
<p>The constructor of Ext.Component saves the config passed to it as <code>initialConfig</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #006600; font-style: italic;">/**
     * This Component's initial configuration specification. Read-only.
     * @type Object
     * @property initialConfig
     */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initialConfig</span> <span style="color: #339933;">=</span> config<span style="color: #339933;">;</span>
&nbsp;
    Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addEvents</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/* abbreviated for clarity */</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Ext.<span style="color: #660066;">ComponentMgr</span>.<span style="color: #660066;">register</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Ext.<span style="color: #660066;">Component</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">constructor</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">baseAction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">baseAction</span>.<span style="color: #660066;">addComponent</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initComponent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You see what happens? The constructor saves <code>initialConfig</code> <strong>before</strong> <code>initComponent</code> is executed. Thus, all configuration you write in <code>initComponent</code> is not saved. I have overlooked this in first versions of my templates and examples mainly because there is only a couple of classes that refer to initialConfig and even in these classes the absence of properly saved <code>initialConfig</code> causes problems very rarely. These Ext classes refer to <code>initialConfig</code>:
</p>
<ul>
<li>AnchorLayout</li>
<li>BorderLayout</li>
<li>Action</li>
<li>GridPanel</li>
<li>Tip</li>
<li>Combo</li>
<li>Form</li>
</ul>
<p>Now, I have updated all my examples, extensions, templates and main site to include this &#8220;magic&#8221; pattern:
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// create config</span>
<span style="color: #003366; font-weight: bold;">var</span> config <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// put your config here</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// eo config object</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// apply config</span>
Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> Ext.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initialConfig</span><span style="color: #339933;">,</span> config<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// call parent</span>
YourExtension.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">initComponent</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><em>The Rule:</em> <strong>Ensure that your extension saves <code>initialConfig</code>.</strong>
</p>
<h2 style="margin-top:30px">listeners</h2>
<p>
If you try to install event handlers by setting property <code>listeners</code> in your <code>config</code> they will not work. Why? The answer lies again in the order of actions in Ext.Component constructor:
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">    Ext.<span style="color: #660066;">Component</span>.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">constructor</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">baseAction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">baseAction</span>.<span style="color: #660066;">addComponent</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initComponent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
As you can see, the constructor calls its parent, that is <code>Ext.util.Observable</code> <strong>before</strong> <code>initComponent</code>. <code>Ext.util.Observable</code> constructor executes:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">util</span>.<span style="color: #660066;">Observable</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">listeners</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">listeners</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">delete</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">listeners</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Any listeners set in <code>initComponent</code> are thus ignored.</p>
<p>There is an easy workaround. Put <code>constructor</code> method in your extension:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
 constructor<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// parent constructor call pre-processing - configure listeners here</span>
    config <span style="color: #339933;">=</span> config <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    config.<span style="color: #660066;">listeners</span> <span style="color: #339933;">=</span> config.<span style="color: #660066;">listeners</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    Ext.<span style="color: #660066;">applyIf</span><span style="color: #009900;">&#40;</span>config.<span style="color: #660066;">listeners</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// add listeners config here</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// call parent constructor</span>
    AnExtension.<span style="color: #660066;">superclass</span>.<span style="color: #660066;">constructor</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// parent constructor call post-processing</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// eo function constructor</span></pre></td></tr></table></div>

<p>and define your listeners therein.</p>
<p><em>The Rule:</em> <strong>Define listeners in <code>constructor</code> method.</strong></p>
<h1>Conclusion</h1>
<p>If you decide to use my way of writing a big application then follow these rules:</p>
<ol>
<li>Do Copy&amp;Paste but always with full understanding of what the code does.</li>
<li>Always check if the Ext class you are going to pre-configure inherits from Ext.Component. You have to use an another approach if it does not.</li>
<li>Never forget to call the parent method, unless you exactly know what you are doing.</li>
<li>Ensure that your extension saves <code>initialConfig</code>.</li>
<li>Define listeners in <code>constructor</code> method.</li>
</ol>
<p>
Happy extending!
</p>
<p><strong><em>Do not forget to read <a href="http://blog.extjs.eu/know-how/writing-a-big-application-in-ext/">Part 1</a> and <a href="http://blog.extjs.eu/know-how/writing-a-big-application-in-ext-part-3/">Part 3</a> of this article. </em></strong><br />
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="js02@aariadne.com" /><input type="hidden" name="item_number" value="Blog Donation" /><input type="hidden" name="currency_code" value="EUR" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/know-how/writing-a-big-application-in-ext-part-2/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Which events are fired?</title>
		<link>http://blog.extjs.eu/know-how/which-events-are-fired/</link>
		<comments>http://blog.extjs.eu/know-how/which-events-are-fired/#comments</comments>
		<pubDate>Sun, 18 May 2008 09:13:54 +0000</pubDate>
		<dc:creator>Saki</dc:creator>
				<category><![CDATA[Know-how]]></category>
		<category><![CDATA[extjs]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.extjs.eu/?p=73</guid>
		<description><![CDATA[<br/>We need to know all events that are fired by a component and in which order sometimes. It is easy to find out.
Let&#8217;s say that the component in question has id my-comp. Load the page with the component, open Firebug, Console tab, and type:

Ext.util.Observable.capture&#40;Ext.getCmp&#40;'my-comp'&#41;, console.info&#41;

Now do some actions with the component and watch the Firebug [...]]]></description>
			<content:encoded><![CDATA[<br/><p>We need to know all events that are fired by a component and in which order sometimes. It is easy to find out.</p>
<p>Let&#8217;s say that the component in question has id <b><code>my-comp</code></b>. Load the page with the component, open Firebug, Console tab, and type:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Ext.<span style="color: #660066;">util</span>.<span style="color: #660066;">Observable</span>.<span style="color: #660066;">capture</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">getCmp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'my-comp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> console.<span style="color: #660066;">info</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Now do some actions with the component and watch the Firebug output.<br />
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><div class="paypal-donations"><input type="hidden" name="cmd" value="_donations" /><input type="hidden" name="business" value="js02@aariadne.com" /><input type="hidden" name="item_number" value="Blog Donation" /><input type="hidden" name="currency_code" value="EUR" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-butcc-donate.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /><img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /></div></form></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.extjs.eu/know-how/which-events-are-fired/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
