<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Eduzine© - RIA</title>
    <link>http://eduzine.edujini-labs.com/</link>
    <description>Articles from Edujini Team</description>
    <dc:language>en</dc:language>
    <admin:errorReportsTo rdf:resource="mailto:" />
    <generator>Serendipity 0.8.2 - http://www.s9y.org/</generator>
    <pubDate>Wed, 18 Jun 2008 07:43:08 GMT</pubDate>

    <image>
        <url>http://www.edujini-labs.com/images/newmasthead.gif</url>
        <title>RSS: Eduzine© - RIA - Articles from Edujini Team</title>
        <link>http://eduzine.edujini-labs.com/</link>
        <width></width>
        <height></height>
    </image>
<item>
    <title>Exploring OOPS - JavaScript Style: Part 3 - Polymorphism</title>
    <link>http://eduzine.edujini-labs.com/archives/36-Exploring-OOPS-JavaScript-Style-Part-3-Polymorphism.html</link>
<category>JavaScript</category><category>Web 2.0</category><category>RIA</category><category>OOAD</category>    <comments>http://eduzine.edujini-labs.com/archives/36-Exploring-OOPS-JavaScript-Style-Part-3-Polymorphism.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=36</wfw:comment>
    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=36</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
In the third and last article of the series of &amp;quot;Exploring OOPS - JavaScript Style&amp;quot;, we look at polymorphism in JavaScript.
&lt;/p&gt;

&lt;p&gt;
Polymorphism literally means &amp;quot;multiple forms&amp;quot;. In OOAD, it means that an object reference can take different behave differently depending upon the scenario.
&lt;/p&gt;

&lt;p&gt;
Technically, there are two types of polymorphism - compile-time polymorphism and runtime polymorphism. One of the ways to implement compile-time polymorphism is method overloading while runtime polymorphism can be implemented by method overriding.
&lt;/p&gt;

&lt;h2&gt;Downloads&lt;/h2&gt;

&lt;p&gt;
The downloads are available in the &lt;a href=&quot;http://downloads.edujini-labs.com&quot;&gt;Downloads&lt;/a&gt; area.
&lt;/p&gt;

 
&lt;h2&gt;Compile-time Polymorphism&lt;/h2&gt;

&lt;p&gt;
All functions in JavaScript are overloaded by default. Surprised, isn't it? Let's take a simple code below for analysis:
&lt;/p&gt;

&lt;pre&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;OOPS in JavaScript - Polymorphism&amp;lt;/code&amp;gt;
    &amp;lt;script language='javascript' type='text/javascript'&amp;gt;
      /**
       &lt;strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
       &lt;/strong&gt;
       &lt;strong&gt; Downloads available at http://downloads.edujini-labs.com
       &lt;/strong&gt;
       &lt;strong&gt; (C) 2008, Edujini Labs Pvt Ltd
       &lt;/strong&gt; http://www.edujini-labs.com
       &lt;strong&gt;/

      function addMethod(x, y)
      {
        document.writeln('x: ' + x + ', y: ' + y);
      }
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
  &amp;lt;pre&amp;gt;&amp;lt;script language='javascript' type='text/javascript'&amp;gt;
      addMethod();
      addMethod(1);
      addMethod(1, 2);
      addMethod(1, 2, 3);
      addMethod(1, 2, 3, 4);
    &amp;lt;/script&amp;gt;
  &amp;lt;/pre&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
For the time being, do not worry about the actual results. What is more important is that invocation of these methods do not result in any error whatsoever! And that's because all methods in JavaScript are, by default, overloaded. And did somebody say - that's compile-time polymorphism?
&lt;/p&gt;

&lt;p&gt;
What we provide in the parameter-list to a JavaScript function is just names to those parameters so that it is convenient to access them. All parameters passed to a function are available through an automatically-created local-variable &lt;code&gt;arguments&lt;/code&gt;. Let's redefine the same method to access all parameters passed to it, convert each into number and return the their sum (if they are not NaN).
&lt;/p&gt;

&lt;pre&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;OOPS in JavaScript - Polymorphism&amp;lt;/code&amp;gt;
    &amp;lt;script language='javascript' type='text/javascript'&amp;gt;
      /**
       &lt;/strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
       &lt;strong&gt;
       &lt;/strong&gt; Downloads available at http://downloads.edujini-labs.com
       &lt;strong&gt;
       &lt;/strong&gt; (C) 2008, Edujini Labs Pvt Ltd
       &lt;strong&gt; http://www.edujini-labs.com
       &lt;/strong&gt;/

      function addMethod(x, y)
      {
        &lt;b&gt;var rv = 0;
        var length = arguments.length;
        var n;

        for(var i = 0; i &amp;lt; length; i++)
        {
          n = Number(arguments[i]);
          if(!isNaN(n))
          {
            rv += n;
          }
        }
        return rv;&lt;/b&gt;
      }
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
  &amp;lt;pre&amp;gt;&amp;lt;script language='javascript' type='text/javascript'&amp;gt;
      &lt;b&gt;document.writeln(addMethod());
      document.writeln(addMethod(1));
      document.writeln(addMethod(1, 2));
      document.writeln(addMethod(1, 2, 3));
      document.writeln(addMethod(1, 2, 3, 4));&lt;/b&gt;
    &amp;lt;/script&amp;gt;
  &amp;lt;/pre&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
&lt;code&gt;arguments&lt;/code&gt; if of the type &lt;a href=&quot;http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Functions:arguments&quot;&gt;&lt;code&gt;Arguments&lt;/code&gt;&lt;/a&gt;. Apart from other properties, one critical thing to which it provides access to is all the parameters passed to the function while invoking it.
&lt;/p&gt;

&lt;p&gt;
Though it may sound unusual for those who are used to working with languages like C, C++, Java, C# etc but probably they can relate this to &lt;code&gt;varargs&lt;/code&gt;. And the same mechanism is available for instance methods (for custom types) as well.
&lt;/p&gt;

&lt;h2&gt;Runtime Polymorphism&lt;/h2&gt;

&lt;p&gt;
Method overriding? Haven't we already discussed this in the previous article while &lt;a href=&quot;http://eduzine.edujini-labs.com/archives/35-Exploring-OOPS-JavaScript-Style-Part-2-Inheritance.html&quot;&gt;discussing about inheritance&lt;/a&gt;?
&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;
In this article we learnt about polymorphism in JavaScript.
&lt;/p&gt;

    </content:encoded>
    <pubDate>Wed, 18 Jun 2008 00:34:10 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/36-guid.html</guid>
    </item>
<item>
    <title>Exploring OOPS - JavaScript Style: Part 2 - Inheritance</title>
    <link>http://eduzine.edujini-labs.com/archives/35-Exploring-OOPS-JavaScript-Style-Part-2-Inheritance.html</link>
<category>JavaScript</category><category>Web 2.0</category><category>RIA</category>    <comments>http://eduzine.edujini-labs.com/archives/35-Exploring-OOPS-JavaScript-Style-Part-2-Inheritance.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=35</wfw:comment>
    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=35</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
In the second article of the series of &amp;quot;Exploring OOPS - JavaScript Style&amp;quot;, we look at how inheritance is implemented in JavaScript.
&lt;/p&gt;

&lt;p&gt;
Inheritance can be described as reusability by virtue of heritage. An inherited data-type automatically obtains the functionality defined in the data-types from which it inherits.
&lt;/p&gt;

&lt;p&gt;
JavaScript supports single inheritance, i.e., you can inherit from maximum one type. Also, it does not support the concept of interface introduced in languages like Java, C# to support multiple inheritance.
&lt;/p&gt;

&lt;p&gt;
In JavaScript what we have is instance-inheritance (or runtime inheritance) rather than class-inheritance (or compile-time inheritance) making inheritance more powerful than probably any other language around (barring the ones like Smalltalk)!
&lt;/p&gt;

&lt;h2&gt;Downloads&lt;/h2&gt;

&lt;p&gt;
The downloads are available in the &lt;a href=&quot;http://downloads.edujini-labs.com&quot;&gt;Downloads&lt;/a&gt; area.
&lt;/p&gt;
 
&lt;h2&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;
In &lt;a href=&quot;http://eduzine.edujini-labs.com/archives/34-Exploring-OOPS-JavaScript-Style-Part-1-Encapsulation.html&quot;&gt;Part 1&lt;/a&gt;, we defined &lt;code&gt;UserProfile&lt;/code&gt; with &lt;code&gt;username&lt;/code&gt; and &lt;code&gt;password&lt;/code&gt;. In this article, we create &lt;code&gt;EmployeeProfile&lt;/code&gt; that inherits from &lt;code&gt;UserProfile&lt;/code&gt; and adds &lt;code&gt;employeeID&lt;/code&gt;. It also reimplements (override, if you like to use that word) &lt;code&gt;authenticate&lt;/code&gt; method.
&lt;/p&gt;

&lt;h2&gt;&lt;code&gt;EmployeeProfile&lt;/code&gt; Definition&lt;/h2&gt;

&lt;p&gt;
We start with defining &lt;code&gt;EmployeeProfile&lt;/code&gt; with &lt;code&gt;employeeID&lt;/code&gt; besides earlier properties. Create a file &lt;code&gt;EmployeeProfile.js&lt;/code&gt; with the following code:
&lt;/p&gt;

&lt;pre&gt;/**
 &lt;strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
 &lt;/strong&gt;
 &lt;strong&gt; Downloads available at http://downloads.edujini-labs.com
 &lt;/strong&gt;
 &lt;strong&gt; (C) 2008, Edujini Labs Pvt Ltd
 &lt;/strong&gt; http://www.edujini-labs.com
 &lt;strong&gt;/

function EmployeeProfile(username, password, employeeID)
{
  this.username = username;
  this.password = password;
  this.employeeID = employeeID;
}
&lt;/pre&gt;

&lt;p&gt;
The next step is to mark it inherited from &lt;code&gt;UserProfile&lt;/code&gt;. For this, we need to work with the property &lt;code&gt;prototype&lt;/code&gt; that is always available to us whenever we declare a function. (Note that all functions can be used as constructor.) Add the following additional code at the end of the code above:
&lt;/p&gt;

&lt;pre&gt;EmployeeProfile.prototype = new UserProfile();
&lt;/pre&gt;

&lt;p&gt;
Note the following:
&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The inerhitance is defined not through the data-type &lt;code&gt;UserProfile&lt;/code&gt; but through its instance.&lt;/li&gt;
  &lt;li&gt;The instance of &lt;code&gt;UserProfile&lt;/code&gt; has been created without passing any parameters to the method.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Testing the Definition&lt;/h2&gt;

&lt;p&gt;
So, it's time to test the definition so far... Let's create &lt;code&gt;EmployeeProfile.html&lt;/code&gt; with the following content:
&lt;/p&gt;

&lt;pre&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;OOPS in JavaScript - Inheritance&amp;lt;/code&amp;gt;
    &amp;lt;script language='javascript' type='text/javascript' src='UserProfile.js'&amp;gt;&amp;lt;/script&amp;gt;
    &lt;b&gt;&amp;lt;script language='javascript' type='text/javascript' src='EmployeeProfile.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;/b&gt;
    &amp;lt;script language='javascript' type='text/javascript'&amp;gt;
      /**
       &lt;/strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
       &lt;strong&gt;
       &lt;/strong&gt; Downloads available at http://downloads.edujini-labs.com
       &lt;strong&gt;
       &lt;/strong&gt; (C) 2008, Edujini Labs Pvt Ltd
       &lt;strong&gt; http://www.edujini-labs.com
       &lt;/strong&gt;/
      function validateUser()
      {
        &lt;b&gt;var eid = document.getElementById('i').value;&lt;/b&gt;
        var uname = document.getElementById('u').value;
        var pwd = document.getElementById('p').value;

        var e = document.getElementById('result');

        &lt;b&gt;var ep = new EmployeeProfile(uname, pwd, eid);
        e.innerHTML = 'Username: ' + ep.username
          + '&amp;lt;br/&amp;gt;Password: ' + ep.password
          + '&amp;lt;br/&amp;gt;EmployeeID: ' + ep.employeeID
          + '&amp;lt;br/&amp;gt;Authenticate: ' + ep.authenticate();&lt;/b&gt;
      }
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
  Employee ID: &amp;lt;input type='text' name='i' id='i' /&amp;gt;
  &amp;lt;br/&amp;gt;
  Username: &amp;lt;input type='text' name='u' id='u' /&amp;gt;
  &amp;lt;br/&amp;gt;
  Password: &amp;lt;input type='password' name='p' id='p' /&amp;gt;
  &amp;lt;br/&amp;gt;

  &amp;lt;button onclick='validateUser(); return false;'&amp;gt;Login&amp;lt;/button&amp;gt;

  &amp;lt;div id='result'&amp;gt;&amp;lt;/div&amp;gt;

  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
I think the code is self explanatory. What we get is not only the three properties but also automatic definition of &lt;code&gt;authenticate&lt;/code&gt; method for &lt;code&gt;EmployeeProfile&lt;/code&gt;. After all, that's that inheritance is all about. In the definition of &lt;code&gt;EmployeeProfile&lt;/code&gt;, we never mention about it but it is automatically available to it through its parent object &lt;code&gt;UserProfile&lt;/code&gt;.
&lt;/p&gt;

&lt;h2&gt;&lt;code&gt;instanceof&lt;/code&gt; Operator&lt;/h2&gt;

&lt;p&gt;
We'll explore method overriding and other complex (unexplored, not mentioned so far) things in a while. We take a short while to look at &lt;code&gt;instanceof&lt;/code&gt; operator.
&lt;/p&gt;

&lt;p&gt;
It is a binary operator. The left operand is the object-reference and the right operand is the function-definition (data-type reference). The operator returns a boolean value indicating &lt;i&gt;whether the object is an instance of the corresponding type or not&lt;/i&gt;.
&lt;/p&gt;

&lt;pre&gt;if(ep &lt;b&gt;instanceof&lt;/b&gt; UserProfile)
{
  alert('ep is an instance of type UserProfile');
} else
{
  alert('ep is NOT an instance of type UserProfile');
}
&lt;/pre&gt;

&lt;p&gt;
You can add this code to the method &lt;code&gt;validateUser&lt;/code&gt;. In our case, it must return &lt;code&gt;true&lt;/code&gt;. Similarly, &lt;code&gt;&lt;em&gt;obj instanceof Object&lt;/em&gt;&lt;/code&gt; for any non-&lt;code&gt;null&lt;/code&gt; value for &lt;code&gt;obj&lt;/code&gt; will return &lt;code&gt;true&lt;/code&gt;.
&lt;/p&gt;

&lt;h2&gt;Method Reference and Optimization&lt;/h2&gt;

&lt;p&gt;
And a very crutial item before we get back on to our main agenda.
&lt;/p&gt;

&lt;p&gt;
Depending upon how we write the code, the methods may be loaded in memory multiple times. Which is bad for the appliation. We do not want identical piece of code to be loaded in memory multiple times. The code must be loaded only once and be executed in the context of the corresponding object. What am I talking? Update the code for the method &lt;code&gt;validateUser&lt;/code&gt; to as follows:
&lt;/p&gt;

&lt;pre&gt;function validateUser()
{
  var eid = document.getElementById('i').value;
  var uname = document.getElementById('u').value;
  var pwd = document.getElementById('p').value;

  &lt;b&gt;var ep1 = new EmployeeProfile(uname, pwd, eid);
  var ep2 = new EmployeeProfile(uname, pwd, eid);&lt;/b&gt;

  alert('Are references same? ' + (ep1.authenticate == ep2.authenticate));
}
&lt;/pre&gt;

&lt;p&gt;
You are bound to get a &lt;code&gt;false&lt;/code&gt;! It hurts. &lt;img src=&quot;http://eduzine.edujini-labs.com/templates/default/img/emoticons/sad.png&quot; alt=&quot;:-(&quot; style=&quot;display: inline; vertical-align: bottom;&quot; class=&quot;emoticon&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;
Everytime we instantiate &lt;code&gt;EmployeeProfile&lt;/code&gt; (or even &lt;code&gt;UserProfile&lt;/code&gt; for that matter), code for the method &lt;code&gt;authenticate&lt;/code&gt; is loaded into memory. Imagine if we have 1000 instances. Not only the properties are loaded into memory 1000 times, but also the code for the method. It's just too bad.
&lt;/p&gt;

&lt;p&gt;
Let's fix this up...
&lt;/p&gt;

&lt;p&gt;
Update the code for &lt;code&gt;UserProfile&lt;/code&gt; to as follows:
&lt;/p&gt;

&lt;pre&gt;function UserProfile(username, password)
{
  this.username = username;
  this.password = password;
}

&lt;b&gt;UserProfile.prototype.authenticate = function()
{
  if(this.username == 'gvaish' &amp;amp;&amp;amp; this.password == 'edujini')
  {
    return true;
  }
  return false;
}&lt;/b&gt;
&lt;/pre&gt;

&lt;p&gt;
Notice the use of &lt;code&gt;prototype&lt;/code&gt; property once again. That holds the key! That's probably - &lt;i&gt;The Key&lt;/i&gt; property in JavaScript.
&lt;/p&gt;

&lt;p&gt;
Now, execute the test case once again. &lt;code&gt;ep1.authenticate == ep1.authenticate&lt;/code&gt; must return true in this case! Detailed discussion around &lt;code&gt;prototype&lt;/code&gt; is out of the scope of this article... but will have one some time soon.
&lt;/p&gt;

&lt;h2&gt;Method Overriding&lt;/h2&gt;

&lt;p&gt;
Next, we explore how to override the method &lt;code&gt;authenticate&lt;/code&gt;. Update the definition of &lt;code&gt;EmployeeProfile&lt;/code&gt; to as given below:
&lt;/p&gt;

&lt;pre&gt;function EmployeeProfile(username, password, employeeID)
{
  this.username = username;
  this.password = password;
  this.employeeID = employeeID;
}

EmployeeProfile.prototype = new UserProfile();

&lt;b&gt;EmployeeProfile.prototype.authenticate = function()
{
  if(this.employeeID == 123 &amp;amp;&amp;amp; this.username == 'gvaish'
                  &amp;amp;&amp;amp; this.password == 'edujini')
  {
    return true;
  }
  return false;
}&lt;/b&gt;
&lt;/pre&gt;

&lt;p&gt;
Go ahead and authenticate with various compbinations of the &lt;code&gt;employeeID&lt;/code&gt;, &lt;code&gt;username&lt;/code&gt; and &lt;code&gt;password&lt;/code&gt;. Hurray! The method has been overridden (using the line of hierarchy of the object &lt;code&gt;prototype&lt;/code&gt;).
&lt;/p&gt;

&lt;p&gt;
The last thing that we see in method overriding is how to invoke the base-type method in the sub-type method. Well, the solution again lies in &lt;code&gt;prototype&lt;/code&gt;!
&lt;/p&gt;

&lt;p&gt;
Let us define the business logic for an employee's authentication as follows:
&lt;/p&gt;

&lt;ol&gt;
	&lt;li&gt;It checks for the authentication results from &lt;code&gt;UserProfile&lt;/code&gt;. If it has failed, the result is failure.&lt;/li&gt;
	&lt;li&gt;It additionally checks for &lt;code&gt;employeeID&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
This way we keep &lt;code&gt;EmployeeProfile&lt;/code&gt; independent of how the &lt;code&gt;UserProfile&lt;/code&gt; authenticates itself. Modify &lt;code&gt;EmployeeProfile&lt;/code&gt; to as follows:
&lt;/p&gt;

&lt;pre&gt;function EmployeeProfile(username, password, employeeID)
{
  this.username = username;
  this.password = password;
  this.employeeID = employeeID;
}

EmployeeProfile.prototype = new UserProfile();

&lt;b&gt;EmployeeProfile.prototype._authenticate = EmployeeProfile.prototype.authenticate;&lt;/b&gt;

EmployeeProfile.prototype.authenticate = function()
{
  &lt;b&gt;if(this._authenticate())
  {
    return this.employeeID == 123;
  }
  return false;&lt;/b&gt;
}
&lt;/pre&gt;

&lt;p&gt;
Note that this is one of the various possible ways of achieving our target. We create a reference &lt;code&gt;_authenticate&lt;/code&gt; to the original &lt;code&gt;authenticate&lt;/code&gt; method (but did we get it from &lt;code&gt;EmployeeProfile&lt;/code&gt; since we defined it in &lt;code&gt;UserProfile&lt;/code&gt; - that's the magic of &lt;code&gt;prototype&lt;/code&gt; in JavaScript).
&lt;/p&gt;

&lt;p&gt;
Rerun your test case... Wow! We get what we expect...
&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;
In this article we learnt how encapsulation is implemented in JavaScript. To summarize, we explored the following:
&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Inheritance in JavaScript.&lt;/li&gt;
  &lt;li&gt;Use of &lt;code&gt;prototype&lt;/code&gt; property to mark inheritance.&lt;/li&gt;
  &lt;li&gt;Defining new methods in the inherited type.&lt;/li&gt;
  &lt;li&gt;Override existing methods, optionally also calling the original method.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;instanceof&lt;/code&gt; operator to check the underlying data-type inheritance hierarchy.&lt;/li&gt;
&lt;/ul&gt;

    </content:encoded>
    <pubDate>Tue, 17 Jun 2008 21:33:51 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/35-guid.html</guid>
    </item>
<item>
    <title>Exploring OOPS - JavaScript Style: Part 1 - Encapsulation</title>
    <link>http://eduzine.edujini-labs.com/archives/34-Exploring-OOPS-JavaScript-Style-Part-1-Encapsulation.html</link>
<category>JavaScript</category><category>Web 2.0</category><category>RIA</category><category>OOAD</category>    <comments>http://eduzine.edujini-labs.com/archives/34-Exploring-OOPS-JavaScript-Style-Part-1-Encapsulation.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=34</wfw:comment>
    <slash:comments>3</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=34</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
As promised in one of the earlier articles, we discuss about how the various pillars of OOPS are implemented in JavaScript.
&lt;/p&gt;

&lt;p&gt;
Obect Oriented Programming - as defined by Grady Booch - stands on three pillars:
&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Encapsulation&lt;/li&gt;
  &lt;li&gt;Inheritance&lt;/li&gt;
  &lt;li&gt;Polymorphism&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
Here, we explore how all three - Yes! All Three Pillars of OOPS - are implemented in JavaScript.
&lt;/p&gt;

&lt;h2&gt;Downloads&lt;/h2&gt;

&lt;p&gt;
The downloads are available in the &lt;a href=&quot;http://downloads.edujini-labs.com&quot;&gt;Downloads&lt;/a&gt; area.
&lt;/p&gt;

 
&lt;h2&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;
To get started, it is assumed that you are already familiar with basic JavaScript. If not you can look on the web for some tutorials on JavaScript 101 tutorials or the like. To be specific, the following fundamentals are assume:
&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Declaring variables - &lt;code&gt;var&lt;/code&gt; keyword.&lt;/li&gt;
  &lt;li&gt;Basic data types supported - &lt;code&gt;Object&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;Number&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;Function&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Writing functions - well, you got to be really very strong in functions!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;4-Sutras while working with JavaScript&lt;/h2&gt;

&lt;p&gt;
While working with JavaScript, always keep in mind the following, as &lt;a href=&quot;http://www.mastergaurav.com/&quot;&gt;Gaurav Vaish&lt;/a&gt; calls them, &lt;a href=&quot;http://blogs.mastergaurav.com/2008/06/16/4-sutras-to-work-with-javascript/&quot;&gt;&amp;quot;4-Sutras&amp;quot;&lt;/a&gt;:
&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;All data-types are infinitely flexible.&lt;/li&gt;
  &lt;li&gt;All objects (references; instances) are infinitely extensible.&lt;/li&gt;
  &lt;li&gt;Objects are associative arrays.&lt;/li&gt;
  &lt;li&gt;It's all about functions.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
&amp;quot;In JavaScript, whenever you run into any problem related to implementation or structuring of the code, these four sutras will help you out&amp;quot;, says &lt;a href=&quot;http://blogs.mastergaurav.com/2008/06/16/4-sutras-to-work-with-javascript/&quot;&gt;Gaurav&lt;/a&gt;.
&lt;/p&gt;

&lt;h2&gt;Encapsulation&lt;/h2&gt;

&lt;p&gt;
Encapsulation - in simplest terms - can be defined as putting the related items together. Structures in C is a way to implement encapsulation. Classes in C++, Java, C# etc is another way to implement encapsulation.
&lt;/p&gt;

&lt;p&gt;
Let us see how encapsulation is implemented in JavaScript.
&lt;/p&gt;

&lt;h2&gt;Welcome, Functions!&lt;/h2&gt;

&lt;p&gt;
Gaurav's 4&lt;sup&gt;th&lt;/sup&gt; Sutra comes into action here - &amp;quot;It's all about functions&amp;quot;.
&lt;/p&gt;

&lt;p&gt;
Let us define a data-type &lt;code&gt;UserProfile&lt;/code&gt; that encapsulates the following three items:
&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code&gt;username&lt;/code&gt;: Property to hold the username of the user.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;password&lt;/code&gt;: Property to hold the password of the user.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;authenticate&lt;/code&gt;: Method to authenticate the user. Returns &lt;code&gt;true&lt;/code&gt; if successful, &lt;code&gt;false&lt;/code&gt; otherwise.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
Let us implement the &lt;code&gt;UserProfile&lt;/code&gt; with a constructor that takes the two properties as parameters and initializes itself. Create a file &lt;code&gt;UserProfile.js&lt;/code&gt; (strictly speaking, the filename doesn't really matter) with the following code.
&lt;/p&gt;

&lt;pre&gt;/**&lt;br /&gt; &lt;strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.&lt;br /&gt; &lt;/strong&gt;&lt;br /&gt; &lt;strong&gt; Downloads available at http://downloads.edujini-labs.com&lt;br /&gt; &lt;/strong&gt;&lt;br /&gt; &lt;strong&gt; (C) 2008, Edujini Labs Pvt Ltd&lt;br /&gt; &lt;/strong&gt; http://www.edujini-labs.com&lt;br /&gt; &lt;strong&gt;/&lt;br /&gt;&lt;br /&gt;function UserProfile(username, password)&lt;br /&gt;{&lt;br /&gt;  this.username = username;&lt;br /&gt;  this.password = password;&lt;br /&gt;&lt;br /&gt;  this.authenticate = function()&lt;br /&gt;  {&lt;br /&gt;    if(this.username == 'gvaish' &amp;amp;&amp;amp; this.password == 'edujini')&lt;br /&gt;    {&lt;br /&gt;      return true;&lt;br /&gt;    }&lt;br /&gt;    return false;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;
Notice the use of &lt;code&gt;this&lt;/code&gt; keyword. Does it remind you of someting? Yes! You got it right... &lt;code&gt;function&lt;/code&gt; is the way to implement the concept of &lt;code&gt;class&lt;/code&gt;, or to speak - encapsulation.
&lt;/p&gt;

&lt;h2&gt;Test Case&lt;/h2&gt;

&lt;p&gt;
Ok... so, how do we use this new data-type that we just defined? Let's create a HTML file that will serve as the playground for our case-studies around the &lt;code&gt;UserProfile&lt;/code&gt;. Create a file &lt;code&gt;UserProfile.html&lt;/code&gt; (again, filename is not important at all) with the following content:
&lt;/p&gt;

&lt;pre&gt; 1 &amp;lt;html&amp;gt;&lt;br /&gt; 2   &amp;lt;head&amp;gt;&lt;br /&gt; 3     &amp;lt;title&amp;gt;OOPS in JavaScript- Encapsulation&amp;lt;/code&amp;gt;&lt;br /&gt; 4     &amp;lt;script language='javascript' type='text/javascript' src='UserProfile.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt; 5     &amp;lt;script language='javascript' type='text/javascript'&amp;gt;&lt;br /&gt; 6       /**&lt;br /&gt; 7        &lt;/strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.&lt;br /&gt; 8        &lt;strong&gt;&lt;br /&gt; 9        &lt;/strong&gt; Downloads available at http://downloads.edujini-labs.com&lt;br /&gt;10        &lt;strong&gt;&lt;br /&gt;11        &lt;/strong&gt; (C) 2008, Edujini Labs Pvt Ltd&lt;br /&gt;12        &lt;strong&gt; http://www.edujini-labs.com&lt;br /&gt;13        &lt;/strong&gt;/&lt;br /&gt;14       function validateUser()&lt;br /&gt;15       {&lt;br /&gt;16         var uname = document.getElementById('u').value;&lt;br /&gt;17         var pwd = document.getElementById('p').value;&lt;br /&gt;18&lt;br /&gt;19         var e = document.getElementById('result');&lt;br /&gt;20&lt;br /&gt;21         var u = new UserProfile(uname, pwd);&lt;br /&gt;22         var result = u.authenticate();&lt;br /&gt;23         e.innerHTML = 'Result: ' + result;&lt;br /&gt;24       }&lt;br /&gt;25     &amp;lt;/script&amp;gt;&lt;br /&gt;26   &amp;lt;/head&amp;gt;&lt;br /&gt;27   &amp;lt;body&amp;gt;&lt;br /&gt;28&lt;br /&gt;29   Username: &amp;lt;input type='text' name='u' id='u' /&amp;gt;&lt;br /&gt;30   &amp;lt;br/&amp;gt;&lt;br /&gt;31   Password: &amp;lt;input type='password' name='p' id='p' /&amp;gt;&lt;br /&gt;32   &amp;lt;br/&amp;gt;&lt;br /&gt;33&lt;br /&gt;34   &amp;lt;button onclick='validateUser(); return false;'&amp;gt;Login&amp;lt;/button&amp;gt;&lt;br /&gt;35&lt;br /&gt;36   &amp;lt;div id='result'&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;37&lt;br /&gt;38   &amp;lt;/body&amp;gt;&lt;br /&gt;30 &amp;lt;/html&amp;gt;&lt;/pre&gt;

&lt;h2&gt;Analysis&lt;/h2&gt;

&lt;p&gt;
Let's just analyze the code that we just wrote.
&lt;/p&gt;

&lt;p&gt;
We have designed a simple form comprising of inputs for username and password, and a button to trigger validation.
&lt;/p&gt;

&lt;p&gt;
The function &lt;code&gt;validateUser&lt;/code&gt; defined at line 14-24 triggers the validation by extracting the values of username and password (lines 16-17).
&lt;/p&gt;

&lt;p&gt;
At line 21, &lt;code&gt;UserProfile&lt;/code&gt; is instantiated. Notice a very important thing - the function definition has been used as the constructor. Yes! That's where functions are so important in JavaScript. The functions in JavaScript are higher order functions (similar to that in Smalltalk), that can be used to retain the state.
&lt;/p&gt;

&lt;p&gt;
At line 22, we invoke the method &lt;code&gt;authenticate&lt;/code&gt; that returns a value &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; depending upon what we enter in the form.
&lt;/p&gt;

&lt;p&gt;
So, go ahead and try out with different combinations of username and password, and checkout the results!
&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;
In this article we learnt how encapsulation is implemented in JavaScript. To summarize, we explored the following:
&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;Encapsulation in JavaScript.&lt;/li&gt;
	&lt;li&gt;Use of &lt;code&gt;this&lt;/code&gt; keyword within the type definition.&lt;/li&gt;
	&lt;li&gt;Defining properties and methods in JavaScript.&lt;/li&gt;
&lt;/ul&gt;

    </content:encoded>
    <pubDate>Mon, 16 Jun 2008 21:53:49 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/34-guid.html</guid>
    </item>
<item>
    <title>Creating Custom Widget in Dojo - Part 2: Templated Widgets</title>
    <link>http://eduzine.edujini-labs.com/archives/33-Creating-Custom-Widget-in-Dojo-Part-2-Templated-Widgets.html</link>
<category>Dojo</category><category>Web 2.0</category><category>RIA</category>    <comments>http://eduzine.edujini-labs.com/archives/33-Creating-Custom-Widget-in-Dojo-Part-2-Templated-Widgets.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=33</wfw:comment>
    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=33</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
In the &lt;a href=&quot;http://eduzine.edujini-labs.com/archives/32-Creating-Custom-Widget-in-Dojo-Part-1.html&quot;&gt;Part 1&lt;/a&gt; of the series, we learnt how to get started with creating custom UI widgets using &lt;a href=&quot;http://dojotoolkit.org&quot;&gt;Dojo Toolkit&lt;/a&gt;. In Part 2, learn to separate the UI from the business logic. Technically, we learn how to use templates to design the UI using simple HTML while keeping the functionality as separate JavaScript code.
&lt;/p&gt;

&lt;h2&gt;Downloads&lt;/h2&gt;

&lt;p&gt;
The downloads, as ever, are available in the &lt;a href=&quot;http://downloads.edujini-labs.com&quot;&gt;downloads area&lt;/a&gt;.
&lt;/p&gt;

&lt;h2&gt;Templated Widgets&lt;/h2&gt;

&lt;p&gt;
Dijit sub-framework in Dojo Toolkit provides a support for templated widgets through the object &lt;code&gt;dijit._Templated&lt;/code&gt;. Well, we do not inherit this but use as a mixin and choose &lt;code&gt;dijit._Widget&lt;/code&gt; as the parent object.
&lt;/p&gt;
 
&lt;h2&gt;Widget Definition&lt;/h2&gt;

&lt;p&gt;
For our purpose of demonstration, we create a simple anchor widget. Yes, it's the &lt;code&gt;&amp;lt;a href='...'&amp;gt;...&amp;lt;/a&amp;gt;&lt;/code&gt; thing!
&lt;/p&gt;

&lt;p&gt;
The widget supports the following properties:
&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;href&lt;/code&gt;: The hyperlink to point to.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;text&lt;/code&gt;: The text to be displayed to the end user.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;target&lt;/code&gt;: The target window for the anchor (Optional).&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;title&lt;/code&gt;: Title for the anchor element (Optional).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Widget Implementation&lt;/h2&gt;

&lt;p&gt;
Assuming that you have the folder layout as in the previous article (&lt;a href=&quot;http://eduzine.edujini-labs.com/archives/32-Creating-Custom-Widget-in-Dojo-Part-1.html&quot;&gt;see here for reference&lt;/a&gt;), create a file &lt;code&gt;/home/edujini/eduzine/dojo/edujini/Anchor.js&lt;/code&gt; with the following contents.
&lt;/p&gt;

&lt;pre&gt;/**
 &lt;strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
 &lt;/strong&gt;
 &lt;strong&gt; Downloads available at http://downloads.edujini-labs.com
 &lt;/strong&gt;
 &lt;strong&gt; (C) 2008, Edujini Labs Pvt Ltd
 &lt;/strong&gt; http://www.edujini-labs.com
 &lt;strong&gt;/

dojo.provide(&amp;quot;edujini.Anchor&amp;quot;);
dojo.require(&amp;quot;dijit._Widget&amp;quot;);
dojo.require(&amp;quot;dijit._Templated&amp;quot;);

dojo.declare(&amp;quot;edujini.Anchor&amp;quot;, [dijit._Widget, dijit._Templated],
{
  text: '',
  href: '',
  target: '',
  title: '',

  templateString  : null,
  templatePath    : dojo.moduleUrl('edujini', 'templates/Anchor.html'),
});
&lt;/pre&gt;

&lt;p&gt;
Notice the following things in the component definition:
&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;The parent object is not a single entry &lt;code&gt;dijit._Wiget&lt;/code&gt; but an array comprising of &lt;code&gt;dijit._Widget&lt;/code&gt; and &lt;code&gt;dijit._Templated&lt;/code&gt;.&lt;/li&gt;
	&lt;li&gt;It contains two properties - &lt;code&gt;templateString&lt;/code&gt; and &lt;code&gt;templatePath&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
The intepretatin of the items in the array is as follows:
&lt;/p&gt;

&lt;ol&gt;
	&lt;li&gt;The first entry indicates the parent object - in this case, &lt;code&gt;dijit._Widget&lt;/code&gt;.&lt;/li&gt;
	&lt;li&gt;The subsequent entries - in this case, only, &lt;code&gt;dijit._Templated&lt;/code&gt; - indicate the objects from where the properties have to be &amp;quot;borrowed&amp;quot; or &amp;quot;overridden&amp;quot;. For example, if both &lt;code&gt;dijit._Widget&lt;/code&gt; and &lt;code&gt;dijit._Templated&lt;/code&gt; have the method &lt;code&gt;buildRendering&lt;/code&gt;, the final definition that will survive is that of &lt;code&gt;dijit._Templated&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Template Definition&lt;/h2&gt;

&lt;p&gt;
How that we have initial definition (components) of the &lt;code&gt;edujini.Anchor&lt;/code&gt; widget ready, let's go ahead and design the layout. Create a file &lt;code&gt;/home/edujini/eduzine/dojo/edujini/templates/Anchor.html&lt;/code&gt; with the following contents:
&lt;/p&gt;

&lt;pre&gt;&amp;lt;span class='edujiniAnchor' dojoAttachPoint='domNode'
  &amp;gt;&amp;lt;a href='${href}' dojoAttachPoint='anchorNode'&amp;gt;${text}&amp;lt;/a&amp;gt;&amp;lt;/span&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
Ensure that there is not trailing newline after &lt;code&gt;&amp;lt;/span&amp;gt;&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;
The interpretation of the layout is as follows:
&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;The complete definition is encapsulated in a &lt;code&gt;span&lt;/code&gt; which comprises of the anchor element.&lt;/li&gt;
	&lt;li&gt;The &lt;code&gt;href&lt;/code&gt; attribute of the anchor element has the value as that of the property &lt;code&gt;href&lt;/code&gt; of the object &lt;code&gt;edujini.Anchor&lt;/code&gt; (Syntax &lt;code&gt;${href}&lt;/code&gt;).&lt;/li&gt;
	&lt;li&gt;The content within the anchor element has the value as that of the property &lt;code&gt;text&lt;/code&gt; of the &lt;code&gt;edujini.Anchor&lt;/code&gt; object.&lt;/li&gt;
	&lt;li&gt;Note that we have still not made use of &lt;code&gt;target&lt;/code&gt; and &lt;code&gt;title&lt;/code&gt; properties. We need to have logic for it!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Test Case&lt;/h2&gt;

&lt;p&gt;
Let's create a file &lt;code&gt;/home/edujini/eduzine/dojo/test-templated-widget.html&lt;/code&gt; with the following content:
&lt;/p&gt;

&lt;pre&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Edujini Tempalted Widget Test Case&amp;lt;/title&amp;gt;

    &amp;lt;script type=&amp;quot;text/javascript&amp;quot; language='javascript' src=&amp;quot;dojo/dojo/dojo.js&amp;quot; djConfig=&amp;quot;isDebug: true, parseOnLoad: true&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script type='text/javascript' language='javascript'&amp;gt;
      dojo.require('dijit.form.Button');
      dojo.require('edujini.Anchor');
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
  &amp;lt;!--
    /**
     &lt;/strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
     &lt;strong&gt;
     &lt;/strong&gt; Downloads available at http://downloads.edujini-labs.com
     &lt;strong&gt;
     &lt;/strong&gt; (C) 2008, Edujini Labs Pvt Ltd
     &lt;strong&gt; http://www.edujini-labs.com
     &lt;/strong&gt;/
  --&amp;gt;
    &amp;lt;div id='a1' text='Hello, Edujini World!' href='http://www.edujini-labs.com' dojoType='edujini.Anchor'&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
Launch the HTML file in the browser... expected UI is:
&lt;br /&gt;
&lt;img border=&quot;1&quot; src=&quot;http://eduzine.edujini-labs.com/uploads/technology/javascript/dojo/33.Dojo.Custom.Widget-2.01.Initial.UI.jpg&quot; alt=&quot;Eduzine - Dojo - Custom Widget - Anchor&quot; title=&quot;Eduzine - Dojo - Custom Widget - Anchor&quot; /&gt;
&lt;/p&gt;


&lt;h2&gt;Finishing Touches&lt;/h2&gt;

&lt;p&gt;
The only functionality that remains is adding &lt;code&gt;target&lt;/code&gt; and/or &lt;code&gt;title&lt;/code&gt;, if provided. For that, we override the method &lt;code&gt;postCreate&lt;/code&gt; in &lt;code&gt;edujini.Anchor&lt;/code&gt;. The updated definition of the object is:
&lt;/p&gt;

&lt;pre&gt;dojo.declare(&amp;quot;edujini.Anchor&amp;quot;, [dijit._Widget, dijit._Templated],
{
  text: '',
  href: '',
  target: '',
  title: '',

  templateString  : null,
  templatePath    : dojo.moduleUrl('edujini', 'templates/Anchor.html'),

  &lt;b&gt;postCreate: function()
  {
    if(this.target)
    {
      this.anchorNode.setAttribute('target', this.target);
    }

    if(this.title)
    {
      this.anchorNode.setAttribute('title', this.title);
    }
  }&lt;/b&gt;
});
&lt;/pre&gt;

&lt;p&gt;
Let's also update the test case:
&lt;/p&gt;

&lt;pre&gt;&amp;lt;div id='a1' text='Hello, Edujini World!'
  &lt;b&gt;target='_edujini'
  title='Edujini Labs Home Page'&lt;/b&gt;
  href='http://www.edujini-labs.com'
  dojoType='edujini.Anchor'&amp;gt;&amp;lt;/div&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
The expected UI is (after updates) is:
&lt;br /&gt;
&lt;img border=&quot;1&quot; src=&quot;http://eduzine.edujini-labs.com/uploads/technology/javascript/dojo/33.Dojo.Custom.Widget-2.02.Updated.UI.jpg&quot; alt=&quot;Eduzine - Dojo - Custom Widget - Anchor&quot; title=&quot;Eduzine - Dojo - Custom Widget - Anchor&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;
In this article, we learnt the following:
&lt;/p&gt;

&lt;ol&gt;
	&lt;li&gt;Create widgets with separated implementation for the layout (JavaScript) and the logic (HTML) - the &amp;quot;templated widgets&amp;quot;.&lt;/li&gt;
	&lt;li&gt;Declare properties in the widget definition (JavaScript) and use them in the template (HTML) - syntax being &lt;code&gt;${&lt;i&gt;property-name&lt;/i&gt;}&lt;/code&gt;.&lt;/li&gt;
	&lt;li&gt;How to set the attributes of the elements (well, that was plain vanilla W3C XML DOM API implementation in JavaScript), conditionally.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Next Steps...&lt;/h2&gt;

&lt;p&gt;
Well, from this side... the next steps would be fulfill the earlier promise of an article on JavaScript - demonstrating the OOPS implementation in JavaScript.
&lt;/p&gt;

    </content:encoded>
    <pubDate>Sat, 14 Jun 2008 00:00:00 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/33-guid.html</guid>
    </item>
<item>
    <title>Creating Custom Widget in Dojo - Part 1</title>
    <link>http://eduzine.edujini-labs.com/archives/32-Creating-Custom-Widget-in-Dojo-Part-1.html</link>
<category>Dojo</category><category>Ajax</category><category>Web 2.0</category><category>RIA</category>    <comments>http://eduzine.edujini-labs.com/archives/32-Creating-Custom-Widget-in-Dojo-Part-1.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=32</wfw:comment>
    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=32</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;p&gt;
Did you know that the &lt;a href=&quot;http://blogs.edujini-labs.com&quot;&gt;Edujini Gang&lt;/a&gt; is working on Dojo for more than a year now! Surprising right? Since there has been not a single entry on Dojo until the recent the &lt;a href=&quot;http://eduzine.edujini-labs.com/archives/29-Dojo-on-Google-Web-Toolkit.html&quot;&gt;recent announcement&lt;/a&gt; of the Project DWT (Dojo on Google Web Toolkit).
&lt;/p&gt;

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
&lt;a href=&quot;http://dojotoolkit.org&quot;&gt;Dojo Toolkit&lt;/a&gt; offers a simple yet powerful architecture to be able to create custom widgets. In Part 1 of the series of articles, we look at how to create a custom widget - probably with minimal styling and functionality.
&lt;/p&gt;

&lt;h2&gt;Downloads&lt;/h2&gt;

&lt;p&gt;
Yo! Buddy. The downloads are available in the &lt;a href=&quot;http://downloads.edujini-labs.com&quot;&gt;downloads area&lt;/a&gt;.
&lt;/p&gt;

 
&lt;h2&gt;What you should know...&lt;/h2&gt;

&lt;p&gt;
Well, you should be very comfortable with the following:
&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;JavaScript - By the way, did you know that JavaScript is Object Oriented? No? Ok... Will provide some articles soon.&lt;/li&gt;
  &lt;li&gt;Defining custom objects (I will not use the term class) in JavaScript using Dojo Toolkit. If not, you can have a peek in at &lt;a href=&quot;http://api.dojotoolkit.org/jsdoc/dojo/HEAD/dojo.declare&quot;&gt;Dojo Documentation&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;
Well, the first thing that you will need it Dojo Toolkit itself. You can download it from &lt;a href=&quot;http://dojotoolkit.org/downloads&quot;&gt;Dojo Downloads&lt;/a&gt;. And then, just a simple vanilla text editor - Notepad, TextPad, EditPlus, Notepad++ - just anything. &lt;img src=&quot;http://eduzine.edujini-labs.com/templates/default/img/emoticons/smile.png&quot; alt=&quot;:-)&quot; style=&quot;display: inline; vertical-align: bottom;&quot; class=&quot;emoticon&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;Widget Definition&lt;/h2&gt;

&lt;p&gt;
Let us create a simple widget that will just say &amp;quot;Hello, World!&amp;quot; (hard-coded) to start with, and subsequently, the content will be customizable.
&lt;/p&gt;


&lt;h2&gt;Widget Implementation&lt;/h2&gt;

&lt;p&gt;
I assume that the Dojo Toolkit library (downloaded earlier) has been extracted in the folder &lt;code&gt;/home/edujini/eduzine/dojo&lt;/code&gt;. Let us create another folder &lt;code&gt;/home/edujini/eduzine/dojo/edujini&lt;/code&gt;. I will not discuss, here in this article, on how to specify a custom location for custom definitions in Dojo. That is out of scope of this article.
&lt;/p&gt;

&lt;p&gt;
Within this sub-folder, create a file &lt;code&gt;CustomWidget.js&lt;/code&gt; with the following contents:
&lt;/p&gt;

&lt;pre&gt;/**&lt;br /&gt; &lt;strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.&lt;br /&gt; &lt;/strong&gt;&lt;br /&gt; &lt;strong&gt; Downloads available at http://downloads.edujini-labs.com&lt;br /&gt; &lt;/strong&gt;&lt;br /&gt; &lt;strong&gt; (C) 2008, Edujini Labs Pvt Ltd&lt;br /&gt; &lt;/strong&gt; http://www.edujini-labs.com&lt;br /&gt; &lt;strong&gt;/&lt;br /&gt;&lt;br /&gt;dojo.provide('edujini.CustomWidget');&lt;br /&gt;dojo.require('dijit._Widget');&lt;br /&gt;&lt;br /&gt;dojo.declare(&amp;quot;edujini.CustomWidget&amp;quot;, dijit._Widget,&lt;br /&gt;{&lt;br /&gt;  postCreate : function()&lt;br /&gt;  {&lt;br /&gt;    this.domNode.innerHTML = 'Hello, World!';&lt;br /&gt;  }&lt;br /&gt;});&lt;/pre&gt;

&lt;p&gt;
One of the more important functions of the object &lt;code&gt;dijit._Widget&lt;/code&gt; is &lt;code&gt;postCreate&lt;/code&gt;. We override the method to provide our implementation and that is to simply set the &lt;code&gt;innerHTML&lt;/code&gt; of the container to the text &lt;code&gt;Hello, World!&lt;/code&gt;.
&lt;/p&gt;

&lt;h2&gt;Using Custom Widget&lt;/h2&gt;

&lt;p&gt;
Ok... now that we have got the widget, we do not know how and if it works. So, let's just write a simple test page (HTML) to use it. Create a file &lt;code&gt;/home/edujini/eduzine/custom-widget-test.html&lt;/code&gt; with the following contents.
&lt;/p&gt;

&lt;pre&gt;&amp;lt;html&amp;gt;&lt;br /&gt;  &amp;lt;head&amp;gt;&lt;br /&gt;    &amp;lt;title&amp;gt;Edujini Custom Widget Test&amp;lt;/title&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;script type=&amp;quot;text/javascript&amp;quot; language='javascript' src=&amp;quot;dojo/dojo/dojo.js&amp;quot; djConfig=&amp;quot;isDebug: true, parseOnLoad: true&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;    &amp;lt;script type='text/javascript' language='javascript'&amp;gt;&lt;br /&gt;      dojo.require('dijit.form.Button');&lt;br /&gt;      dojo.require('edujini.CustomWidget');&lt;br /&gt;    &amp;lt;/script&amp;gt;&lt;br /&gt;  &amp;lt;/head&amp;gt;&lt;br /&gt;  &amp;lt;body&amp;gt;&lt;br /&gt;  &amp;lt;!--&lt;br /&gt;    /**&lt;br /&gt;     &lt;/strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.&lt;br /&gt;     &lt;strong&gt;&lt;br /&gt;     &lt;/strong&gt; Downloads available at http://downloads.edujini-labs.com&lt;br /&gt;     &lt;strong&gt;&lt;br /&gt;     &lt;/strong&gt; (C) 2008, Edujini Labs Pvt Ltd&lt;br /&gt;     &lt;strong&gt; http://www.edujini-labs.com&lt;br /&gt;     &lt;/strong&gt;/&lt;br /&gt;  --&amp;gt;&lt;br /&gt;    &amp;lt;div id='w1' dojoType='edujini.CustomWidget'&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;  &amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;

&lt;p&gt;
With this, the response that we expect is something as given below (I've highlighted the output using the red-ellipse. You'll not get it):
&lt;/p&gt;

&lt;p&gt;
&lt;img title=&quot;Eduzine - Dojo - Custom Widget - Simple - Output&quot; alt=&quot;Eduzine - Dojo - Custom Widget - Simple - Output&quot; src=&quot;http://eduzine.edujini-labs.com/uploads/technology/javascript/dojo/32.Dojo.Custom.Widget-1-01.Output.jpg&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;Customizing the Widget&lt;/h2&gt;

&lt;p&gt;
The next step is to customize the &lt;code&gt;Hello, World!&lt;/code&gt; with a user defined content. Let's say that the default value of the content to be displayed is &lt;code&gt;Hello, World!&lt;/code&gt;. To this effect, update the &lt;code&gt;CustomWidget.js&lt;/code&gt; as follows (changes highlighted in bold):
&lt;/p&gt;

&lt;pre&gt;dojo.declare(&amp;quot;edujini.CustomWidget&amp;quot;, dijit._Widget,&lt;br /&gt;{&lt;br /&gt;  &lt;b&gt;text: 'Hello, World!',&lt;/b&gt;

  postCreate : function()
  {
    this.domNode.innerHTML = &lt;b&gt;this.text;&lt;/b&gt;
  }
});
&lt;/pre&gt;

&lt;p&gt;
Simultaneously, we update the &lt;code&gt;custom-widget-test.html&lt;/code&gt; file to as follows:
&lt;/p&gt;

&lt;pre&gt;  &amp;lt;body&amp;gt;&lt;br /&gt;    &amp;lt;div id='w1' &lt;b&gt;text='Hello, Edujini World!'&lt;/b&gt; dojoType='edujini.CustomWidget'&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;  &amp;lt;/body&amp;gt;&lt;/pre&gt;

&lt;p&gt;
And that's it! Look at the HTML file in the browser...
&lt;br /&gt;
&lt;img title=&quot;Eduzine - Dojo - Custom Widget - Simple - Custom Property&quot; alt=&quot;Eduzine - Dojo - Custom Widget - Simple - Custom Property&quot; src=&quot;http://eduzine.edujini-labs.com/uploads/technology/javascript/dojo/32.Dojo.Custom.Widget-1-02.Property.jpg&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;
In this article, we learnt:
&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;To create a custom widget using Dojo Toolkit.&lt;/li&gt;
	&lt;li&gt;To create a property that can be used subsequently in the widget.&lt;/li&gt;
	&lt;li&gt;Had a brief venture into the method &lt;code&gt;postCreate&lt;/code&gt; - one of the key methods. It it called after all entries from the HTML have been read and parsed. We shall look into the details of this method later on when we explore the lifecycle of Dojo Widgets.&lt;/li&gt;
&lt;/ul&gt;

    </content:encoded>
    <pubDate>Thu, 12 Jun 2008 00:00:00 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/32-guid.html</guid>
    </item>
<item>
    <title>Flex Data Driven Controls - Part 2</title>
    <link>http://eduzine.edujini-labs.com/archives/31-Flex-Data-Driven-Controls-Part-2.html</link>
<category>Web 2.0</category><category>RIA</category><category>Flex</category><category>ActionScript</category>    <comments>http://eduzine.edujini-labs.com/archives/31-Flex-Data-Driven-Controls-Part-2.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=31</wfw:comment>
    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=31</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
In Part 2 of the series of articles on &amp;quot;Flex Data Driven Controls&amp;quot;, we learn about how to provide custom rendering to a data bound control. And we choose &lt;code&gt;List&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
In the previous article, &lt;a href=&quot;http://eduzine.edujini-labs.com/archives/30-Flex-Data-Driven-Controls-Part-1.html&quot;&gt;Part 1&lt;/a&gt; of the series, we got ourselves started with data driven controls - &lt;code&gt;ComboBox&lt;/code&gt; and &lt;code&gt;DataGrid&lt;/code&gt;. In this article, we will lean to cutomize the how the items are finally presented to the end user.
&lt;/p&gt;

&lt;p&gt;
The source code for this article is available in the &lt;a href=&quot;http://downloads.edujini-labs.com&quot;&gt;Downloads&lt;/a&gt; area.
&lt;/p&gt;

 
&lt;h2&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;
We reuse the previous definition of &lt;code&gt;UserProfile&lt;/code&gt;. For completeness, the definition below is re-represented below:
&lt;/p&gt;

&lt;pre&gt;package com.edujinilabs.eduzine.tutorials.flex.core
{
  /**
   &lt;strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
   &lt;/strong&gt;
   &lt;strong&gt; Downloads available at http://downloads.edujini-labs.com
   &lt;/strong&gt;
   &lt;strong&gt; (C) 2008, Edujini Labs Pvt Ltd
   &lt;/strong&gt; http://www.edujini-labs.com
   */
  public class UserProfile
  {
    private var _employeeID : int;
    private var _employeeName: String;
    private var _departmentName: String;

    public function UserProfile(eid: int = 0, name: String = null, deptName: String = null)
    {
      this._employeeID = eid;
      this._employeeName = name;
      this._departmentName = deptName;
    }

    public function get employeeID(): int
    {
      return _employeeID;
    }

    public function set employeeID(eid: int) : void
    {
      this._employeeID = eid;
    }

    public function get employeeName(): String
    {
      return _employeeName;
    }

    public function set employeeName(name: String) : void
    {
      this._employeeName = name;
    }

    public function get departmentName(): String
    {
      return _departmentName;
    }

    public function set departmentName(name: String) : void
    {
      this._departmentName = name;
    }
  }
}
&lt;/pre&gt;

&lt;p&gt;
As far the user interface is concerned, we replace the &lt;code&gt;ComboBox&lt;/code&gt; with &lt;code&gt;List&lt;/code&gt; and remove &lt;code&gt;DataGrid&lt;/code&gt; from the previous MXML definition. There would be a need to customize the layout a bit. The final definition looks as follows (important changes have been marked in bold):
&lt;/p&gt;

&lt;pre&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;
&amp;lt;mx:Application xmlns:mx=&amp;quot;http://www.adobe.com/2006/mxml&amp;quot; layout=&amp;quot;absolute&amp;quot;
  height=&amp;quot;426&amp;quot; width=&amp;quot;400&amp;quot; borderThickness=&amp;quot;2&amp;quot; borderStyle=&amp;quot;solid&amp;quot;
  backgroundColor=&amp;quot;#F0F0F0&amp;quot; cornerRadius=&amp;quot;8&amp;quot;&amp;gt;

  &amp;lt;mx:Label text=&amp;quot;Edujini Labs Pvt Ltd&amp;quot; fontSize=&amp;quot;14&amp;quot; fontFamily=&amp;quot;Georgia&amp;quot;
  	fontWeight=&amp;quot;bold&amp;quot; fontStyle=&amp;quot;normal&amp;quot; color=&amp;quot;#F5A83B&amp;quot; left=&amp;quot;20&amp;quot; top=&amp;quot;20&amp;quot;/&amp;gt;

  &amp;lt;mx:Label x=&amp;quot;20&amp;quot; y=&amp;quot;64&amp;quot; text=&amp;quot;Edujini Team&amp;quot; fontFamily=&amp;quot;Verdana&amp;quot; fontSize=&amp;quot;10&amp;quot; fontWeight=&amp;quot;bold&amp;quot;/&amp;gt;

  &lt;b&gt;&amp;lt;mx:List id='eList' x=&amp;quot;20&amp;quot; y=&amp;quot;90&amp;quot; dataProvider=&amp;quot;{employees}&amp;quot; labelField=&amp;quot;employeeName&amp;quot;
  	cornerRadius=&amp;quot;4&amp;quot; width=&amp;quot;344&amp;quot; height=&amp;quot;265&amp;quot;&amp;gt;&lt;/b&gt;
  &amp;lt;/mx:List&amp;gt;

  &amp;lt;mx:Label x=&amp;quot;20&amp;quot; text=&amp;quot;© 2008, Edujini Labs Pvt Ltd&amp;quot; fontFamily=&amp;quot;Verdana&amp;quot;
  	fontSize=&amp;quot;10&amp;quot; fontWeight=&amp;quot;bold&amp;quot; bottom=&amp;quot;20&amp;quot;/&amp;gt;

  &amp;lt;mx:Script&amp;gt;
    &amp;lt;![CDATA[
      import com.edujinilabs.eduzine.tutorials.flex.core.UserProfile;
      import mx.collections.ArrayCollection;

      [Bindable]
      private var employees: ArrayCollection = new ArrayCollection(
      [
        new UserProfile(11, &amp;quot;Shake Sajan&amp;quot;, &amp;quot;Product Dev&amp;quot;),
        new UserProfile(24, &amp;quot;Meera&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(23, &amp;quot;Neelam&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(12, &amp;quot;Hari S Nair&amp;quot;, &amp;quot;Product Dev&amp;quot;),
        new UserProfile(13, &amp;quot;Abdul Prodhani&amp;quot;, &amp;quot;Product Dev&amp;quot;),
        new UserProfile(21, &amp;quot;Ashish Vaish&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(10, &amp;quot;Gaurav Vaish&amp;quot;, &amp;quot;Technology&amp;quot;),
        new UserProfile(32, &amp;quot;Jagadish&amp;quot;, &amp;quot;Marketing&amp;quot;),
        new UserProfile(33, &amp;quot;Anshu&amp;quot;, &amp;quot;Marketing&amp;quot;),
        new UserProfile(22, &amp;quot;Padma Raviee&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(14, &amp;quot;Rajesh&amp;quot;, &amp;quot;Technology&amp;quot;),
        new UserProfile(31, &amp;quot;Yashwanth C&amp;quot;, &amp;quot;Management&amp;quot;),
        new UserProfile(34, &amp;quot;Ramesh&amp;quot;, &amp;quot;Finance&amp;quot;)
      ]);
    ]]&amp;gt;
  &amp;lt;/mx:Script&amp;gt;
&amp;lt;/mx:Application&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
With this, the UI that we expect is as follows:
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/flex/31.Flex.Data-Driven.Renderer.01.Initial.UI.jpg&quot; alt=&quot;Eduzine - Flex List - Renderer - Initial UI&quot; title=&quot;Eduzine - Flex List - Renderer - Initial UI&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;Flex Item Renderers&lt;/h2&gt;

&lt;p&gt;
This interface is decently fine. Let's get something more out of this. How about change the UI to not only display the employees' names but also their Employee ID and the Department Name. And showing them a little formatted - say, in two lines rather than in a single line.
&lt;/p&gt;

&lt;p&gt;
Adobe Flex data driven controls work internally with the so called item renderers to represent a &amp;quot;list of items&amp;quot;. The external presentation may be linear, grid, tree or a combination of these.
&lt;/p&gt;

&lt;p&gt;
To get started, let us define a simple renderer for our &lt;code&gt;List&lt;/code&gt;. Modify the &lt;code&gt;List&lt;/code&gt; definition to as follows:
&lt;/p&gt;

&lt;pre&gt;  &amp;lt;mx:List id='eList' x=&amp;quot;20&amp;quot; y=&amp;quot;90&amp;quot; dataProvider=&amp;quot;{employees}&amp;quot; labelField=&amp;quot;employeeName&amp;quot;
    cornerRadius=&amp;quot;4&amp;quot; width=&amp;quot;344&amp;quot; height=&amp;quot;265&amp;quot;&amp;gt;
    &lt;b&gt;&amp;lt;mx:itemRenderer&amp;gt;
      &amp;lt;mx:Component&amp;gt;
        &amp;lt;mx:VBox&amp;gt;
          &amp;lt;mx:Label text=&amp;quot;ID: {data.employeeID}&amp;quot; /&amp;gt;
          &amp;lt;mx:HBox width=&amp;quot;100%&amp;quot;&amp;gt;
            &amp;lt;mx:Label text=&amp;quot;Name: {data.employeeName}&amp;quot; width=&amp;quot;50%&amp;quot; /&amp;gt;
            &amp;lt;mx:Label text=&amp;quot;Department: {data.departmentName}&amp;quot; width=&amp;quot;50%&amp;quot; /&amp;gt;
          &amp;lt;/mx:HBox&amp;gt;
        &amp;lt;/mx:VBox&amp;gt;
      &amp;lt;/mx:Component&amp;gt;
    &amp;lt;/mx:itemRenderer&amp;gt;&lt;/b&gt;
  &amp;lt;/mx:List&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
The &lt;code&gt;itemRenderer&lt;/code&gt; comprises of one &lt;code&gt;VBox&lt;/code&gt; with two entries - a &lt;code&gt;Label&lt;/code&gt; to display the Employee ID, and a &lt;code&gt;HBox&lt;/code&gt; comprising of details for Employee Name and the Department Name.
&lt;/p&gt;

&lt;p&gt;
Note that the &lt;code&gt;itemRenderer&lt;/code&gt; element can contain only a definition of the &lt;code&gt;Component&lt;/code&gt; which in turn can comprise of only one component. For example, it is not possible to have a &lt;code&gt;Component&lt;/code&gt; with two &lt;code&gt;Label&lt;/code&gt; elements directly within itself. These &lt;code&gt;Labels&lt;/code&gt; must be enclosed in a container layout, like &lt;code&gt;VBox&lt;/code&gt; in this case.
&lt;/p&gt;

&lt;p&gt;
Also note that the controls are bount to values of the type &lt;code&gt;{data.*}&lt;/code&gt;. &lt;code&gt;data&lt;/code&gt; is a special variable in this case - it refers to the &lt;code&gt;item&lt;/code&gt; being rendered. For each iteration corresponding to the entry in the &lt;code&gt;dataProvider&lt;/code&gt;, &lt;code&gt;data&lt;/code&gt; takes up the corresponding value. The type of the value referred to by &lt;code&gt;data&lt;/code&gt; is same as that in the &lt;code&gt;dataProvider&lt;/code&gt;, which in our case is &lt;code&gt;com.edujinilabs.eduzine.tutorials.flex.core.UserProfile&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
The UI expected is as below:
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/flex/31.Flex.Data-Driven.Renderer.02.Simple.Renderer.jpg&quot; alt=&quot;Eduzine - Flex List - Custom Rendered Definition&quot; title=&quot;Eduzine - Flex List - Custom Rendered Definition&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;Flex Item Editors&lt;/h2&gt;

&lt;p&gt;
Let's further fancy the UI and give the HR department an ease to be able to edit the details inline. When the HR personnel clicks on any entry, it should get into the edit mode and display the items as below:
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Employee ID: Must be shown in a &lt;code&gt;Label&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Employee Name: Must be shown in a &lt;code&gt;TextInput&lt;/code&gt;, so that it may be updated.&lt;/li&gt;
&lt;li&gt;Employee ID: Must be shown as a &lt;code&gt;ComboBox&lt;/code&gt;, with a list of options available.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
Now that we already know how to data-bind the controls, we can directly jump on to looking at the updated &lt;code&gt;List&lt;/code&gt; definition in the MXML:
&lt;/p&gt;

&lt;pre&gt;  &amp;lt;mx:List id='eList' x=&amp;quot;20&amp;quot; y=&amp;quot;90&amp;quot; dataProvider=&amp;quot;{employees}&amp;quot; labelField=&amp;quot;employeeName&amp;quot;
    cornerRadius=&amp;quot;4&amp;quot; width=&amp;quot;344&amp;quot; height=&amp;quot;265&amp;quot; editable=&amp;quot;true&amp;quot; itemEditEnd=&amp;quot;event.preventDefault()&amp;quot;&amp;gt;
    &amp;lt;mx:itemRenderer&amp;gt;
      &amp;lt;mx:Component&amp;gt;
        &amp;lt;mx:VBox&amp;gt;
          &amp;lt;mx:Label text=&amp;quot;ID: {data.employeeID}&amp;quot; /&amp;gt;
          &amp;lt;mx:HBox width=&amp;quot;100%&amp;quot;&amp;gt;
            &amp;lt;mx:Label text=&amp;quot;Name: {data.employeeName}&amp;quot; width=&amp;quot;50%&amp;quot; /&amp;gt;
            &amp;lt;mx:Label text=&amp;quot;Department: {data.departmentName}&amp;quot; width=&amp;quot;50%&amp;quot; /&amp;gt;
          &amp;lt;/mx:HBox&amp;gt;
        &amp;lt;/mx:VBox&amp;gt;
      &amp;lt;/mx:Component&amp;gt;
    &amp;lt;/mx:itemRenderer&amp;gt;
    &lt;b&gt;&amp;lt;mx:itemEditor&amp;gt;
      &amp;lt;mx:Component&amp;gt;
        &amp;lt;mx:VBox&amp;gt;
          &amp;lt;mx:Label text=&amp;quot;ID: {data.employeeID}&amp;quot; /&amp;gt;
          &amp;lt;mx:HBox width=&amp;quot;100%&amp;quot;&amp;gt;
            &amp;lt;mx:TextInput text=&amp;quot;{data.employeeName}&amp;quot; width=&amp;quot;50%&amp;quot; /&amp;gt;
            &amp;lt;mx:ComboBox width=&amp;quot;50%&amp;quot; selectedItem=&amp;quot;{data.departmentName}&amp;quot;&amp;gt;
              &amp;lt;mx:dataProvider&amp;gt;
                &amp;lt;mx:String&amp;gt;Product Dev&amp;lt;/mx:String&amp;gt;
                &amp;lt;mx:String&amp;gt;Training&amp;lt;/mx:String&amp;gt;
                &amp;lt;mx:String&amp;gt;Technology&amp;lt;/mx:String&amp;gt;
                &amp;lt;mx:String&amp;gt;Finance&amp;lt;/mx:String&amp;gt;
                &amp;lt;mx:String&amp;gt;Management&amp;lt;/mx:String&amp;gt;
              &amp;lt;/mx:dataProvider&amp;gt;
            &amp;lt;/mx:ComboBox&amp;gt;
          &amp;lt;/mx:HBox&amp;gt;
        &amp;lt;/mx:VBox&amp;gt;
      &amp;lt;/mx:Component&amp;gt;
    &amp;lt;/mx:itemEditor&amp;gt;&lt;/b&gt;
  &amp;lt;/mx:List&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
The updates in the code, now, must be self explanatory. You would also notice alternate form of specifying the &lt;code&gt;dataProvider&lt;/code&gt; for the &lt;code&gt;ComboBox&lt;/code&gt;. If you select any entry in the list, it must get into the edit-mode. The expected UI is as given below:
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/flex/31.Flex.Data-Driven.Renderer.03.Item.Editor.jpg&quot; alt=&quot;Eduzine - Flex List - Custom Item Editor Definition&quot; title=&quot;Eduzine - Flex List - Custom Item Editor Definition&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;
So, we seem to have got everything... except that the names / department names, if updated, do not effect the original items (the model). We shall explore this in our next article.
&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;
In this article, we learnt to:
&lt;/p&gt;&lt;ol&gt;
 &lt;li&gt;Use Flex Item Renderers in data driven controls to create a rich UI to render the business objects.&lt;/li&gt;
 &lt;li&gt;Use Flex Item Editors to edit the business objects. We have left the task of updating the business objects to the next article.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;

&lt;/p&gt;    </content:encoded>
    <pubDate>Tue, 10 Jun 2008 10:19:00 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/31-guid.html</guid>
    </item>
<item>
    <title>Flex Data Driven Controls - Part 1</title>
    <link>http://eduzine.edujini-labs.com/archives/30-Flex-Data-Driven-Controls-Part-1.html</link>
<category>Web 2.0</category><category>RIA</category><category>Flex</category><category>ActionScript</category>    <comments>http://eduzine.edujini-labs.com/archives/30-Flex-Data-Driven-Controls-Part-1.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=30</wfw:comment>
    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=30</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
Data driven controls are key to any UI toolkit, and Adobe Flex is no different.
&lt;/p&gt;

&lt;p&gt;
In this article, we explore some data driven controls in Flex and look at their basic functionalities, especially the data binding and custom rendering.
&lt;/p&gt;

&lt;p&gt;
In case you need a professional support on RIA, Web 2.0 and/or Adobe Flex, feel free to mail
&lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot;&gt;
  (function()
  {
    var _ej = 'y' + 'a' + 's' + 'h' + 'w' + 'a' + 'n' + 't' + 'h' + '@' + 'edujini' + '-' + 'labs' + '.com';
    document.write('&lt;a href=&quot;mailto:' + _ej + '?Subject=Flex / Web 2.0 Support&quot;&gt;' + _ej + '&lt;/a&gt;');
  })();
&lt;/script&gt;
&lt;/p&gt;

&lt;p&gt;
Downloads are available in the &lt;a href=&quot;http://downloads.edujini-labs.com&quot;&gt;Downloads&lt;/a&gt; section.
&lt;/p&gt;

 
&lt;h2&gt;The Scenario&lt;/h2&gt;

&lt;p&gt;
Let's take a fictitious but practical scenario. The HR department at Edujini Labs to be given a dashboard to:
&lt;/p&gt;&lt;ul&gt;
 &lt;li&gt;Display employee names&lt;/li&gt;
 &lt;li&gt;Dispaly employee details&lt;/li&gt;
&lt;/ul&gt;
&lt;p /&gt;

&lt;p&gt;
Other complexities like capabilities to edit the employee details, paginate the listing if the list is long et al can be added later on.
&lt;/p&gt;

&lt;h2&gt;Getting Started...&lt;/h2&gt;

&lt;p&gt;
To get started, let's freeze on the following UI:
&lt;/p&gt;&lt;ul&gt;
 &lt;li&gt;For the employee list, we use a simple &lt;a href=&quot;http://livedocs.adobe.com/flex/3/langref/mx/controls/ComboBox.html&quot;&gt;ComboBox&lt;/a&gt; to get started with. Later we'd explore other options&lt;/li&gt;
 &lt;li&gt;For the details, we use &lt;a href=&quot;http://livedocs.adobe.com/flex/3/langref/mx/controls/DataGrid.html&quot;&gt;DataGrid&lt;/a&gt;.
&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;

&lt;/p&gt;&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/flex/30.Flex.Data-Driven.01.UI.Definition.jpg&quot; alt=&quot;flex - datagrid/combobox - Edujini Labs HR Department UI&quot; title=&quot;flex - datagrid/combobox - Edujini Labs HR Department UI&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;
The above UI can be generated using the following application definition (You will need Adobe Flex Builder to design it WSISYG or you can download the Adobe Flex SDK and use the MXML compiler to compile it at command line):
&lt;/p&gt;

&lt;pre&gt;&amp;lt;mx:Application xmlns:mx=&amp;quot;http://www.adobe.com/2006/mxml&amp;quot; layout=&amp;quot;absolute&amp;quot;
  height=&amp;quot;280&amp;quot; width=&amp;quot;600&amp;quot; borderThickness=&amp;quot;2&amp;quot; borderStyle=&amp;quot;solid&amp;quot;
  backgroundColor=&amp;quot;#F0F0F0&amp;quot; cornerRadius=&amp;quot;8&amp;quot;&amp;gt;

  &amp;lt;mx:Label text=&amp;quot;Edujini Labs Pvt Ltd&amp;quot; fontSize=&amp;quot;14&amp;quot;
      fontFamily=&amp;quot;Georgia&amp;quot; fontWeight=&amp;quot;bold&amp;quot; fontStyle=&amp;quot;normal&amp;quot;
      color=&amp;quot;#F5A83B&amp;quot; left=&amp;quot;20&amp;quot; top=&amp;quot;20&amp;quot;/&amp;gt;

  &amp;lt;mx:Label x=&amp;quot;21&amp;quot; y=&amp;quot;64&amp;quot; text=&amp;quot;Edujini Team&amp;quot; fontFamily=&amp;quot;Verdana&amp;quot;
      fontSize=&amp;quot;10&amp;quot; fontWeight=&amp;quot;bold&amp;quot;/&amp;gt;

  &amp;lt;mx:ComboBox x=&amp;quot;21&amp;quot; y=&amp;quot;90&amp;quot;&amp;gt;&amp;lt;/mx:ComboBox&amp;gt;

  &amp;lt;mx:DataGrid x=&amp;quot;218&amp;quot; y=&amp;quot;92&amp;quot; width=&amp;quot;368&amp;quot;&amp;gt;
    &amp;lt;mx:columns&amp;gt;
      &amp;lt;mx:DataGridColumn headerText=&amp;quot;Employee ID&amp;quot; dataField=&amp;quot;employeeID&amp;quot;/&amp;gt;
      &amp;lt;mx:DataGridColumn headerText=&amp;quot;Employee Name&amp;quot; dataField=&amp;quot;employeeName&amp;quot;/&amp;gt;
      &amp;lt;mx:DataGridColumn headerText=&amp;quot;Department&amp;quot; dataField=&amp;quot;departmentName&amp;quot;/&amp;gt;
    &amp;lt;/mx:columns&amp;gt;
  &amp;lt;/mx:DataGrid&amp;gt;

  &amp;lt;mx:Label x=&amp;quot;21&amp;quot; y=&amp;quot;248&amp;quot; text=&amp;quot;© 2008, Edujini Labs Pvt Ltd&amp;quot;
      ontFamily=&amp;quot;Verdana&amp;quot; fontSize=&amp;quot;10&amp;quot; fontWeight=&amp;quot;bold&amp;quot;/&amp;gt;

&amp;lt;/mx:Application&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
Note the definition of the &lt;code&gt;DataGrid&lt;/code&gt;. It contains a bunch of &lt;code&gt;DataGridColumn&lt;/code&gt; definitions. Each &lt;code&gt;DataGridColumn&lt;/code&gt; has been specified a &lt;code&gt;headerText&lt;/code&gt; - header of the column, and &lt;code&gt;dataField&lt;/code&gt; - the property of the object whose value is to be displayed.
&lt;/p&gt;

&lt;p&gt;
To model our data, let's create a class &lt;code&gt;UserProfile&lt;/code&gt; in package, say, &lt;code&gt;com.edujinilabs.eduzine.tutorials.flex.core&lt;/code&gt;. It comprises of the Employee ID, Employee Name and the Department Name.
&lt;/p&gt;

&lt;pre&gt;package com.edujinilabs.eduzine.tutorials.flex.core
{
  /**
   &lt;strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
   &lt;/strong&gt; Downloads available at http://downloads.edujini-labs.com
   &lt;strong&gt;
   &lt;/strong&gt; (C) 2008, Edujini Labs Pvt Ltd
   &lt;strong&gt; http://www.edujini-labs.com
   &lt;/strong&gt;/
  public class UserProfile
  {
    private var _employeeID : int;
    private var _employeeName: String;
    private var _departmentName: String;

    public function UserProfile(eid: int = 0,
                           name: String = null, deptName: String = null)
    {
      this._employeeID = eid;
      this._employeeName = name;
      this._departmentName = deptName;
    }

    public function get employeeID(): int
    {
      return _employeeID;
    }

    public function set employeeID(eid: int) : void
    {
      this._employeeID = eid;
    }

    public function get employeeName(): String
    {
      return _employeeName;
    }

    public function set employeeName(name: String) : void
    {
      this._employeeName = name;
    }

    public function get departmentName(): String
    {
      return _departmentName;
    }

    public function set departmentName(name: String) : void
    {
      this._departmentName = name;
    }
  }
}
&lt;/pre&gt;

&lt;p&gt;
Notice that the property names have been purposely chosen to match the values of &lt;code&gt;dataField&lt;/code&gt; of the &lt;code&gt;DataGridColumn&lt;/code&gt;'s defined earlier.
&lt;/p&gt;

&lt;h2&gt;Data Model&lt;/h2&gt;

&lt;p&gt;
Employee list would be represented using the variable &lt;code&gt;employees&lt;/code&gt; of type &lt;code&gt;mx.collections.ArrayCollection&lt;/code&gt;. Ensure that it is &lt;code&gt;Bindable&lt;/code&gt;. Finally, let's popupate it with some data, as given below. Add the code directly into the MXML Application created.
&lt;/p&gt;

&lt;pre&gt;  &amp;lt;mx:Script&amp;gt;
    &amp;lt;![CDATA[
      import com.edujinilabs.eduzine.tutorials.flex.core.UserProfile;
      import mx.collections.ArrayCollection;

      [Bindable]
      private var employees: ArrayCollection = new ArrayCollection(
      [
        new UserProfile(11, &amp;quot;Shake Sajan&amp;quot;, &amp;quot;Product Dev&amp;quot;),
        new UserProfile(24, &amp;quot;Meera&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(23, &amp;quot;Neelam&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(12, &amp;quot;Hari S Nair&amp;quot;, &amp;quot;Product Dev&amp;quot;),
        new UserProfile(13, &amp;quot;Abdul Prodhani&amp;quot;, &amp;quot;Product Dev&amp;quot;),
        new UserProfile(21, &amp;quot;Ashish Vaish&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(10, &amp;quot;Gaurav Vaish&amp;quot;, &amp;quot;Technology&amp;quot;),
        new UserProfile(32, &amp;quot;Jagadish&amp;quot;, &amp;quot;Marketing&amp;quot;),
        new UserProfile(33, &amp;quot;Anshu&amp;quot;, &amp;quot;Marketing&amp;quot;),
        new UserProfile(22, &amp;quot;Padma Raviee&amp;quot;, &amp;quot;Training&amp;quot;),
        new UserProfile(14, &amp;quot;Rajesh&amp;quot;, &amp;quot;Technology&amp;quot;),
        new UserProfile(31, &amp;quot;Yashwanth C&amp;quot;, &amp;quot;Management&amp;quot;),
        new UserProfile(34, &amp;quot;Ramesh&amp;quot;, &amp;quot;Finance&amp;quot;)
      ]);
    ]]&amp;gt;
  &amp;lt;/mx:Script&amp;gt;
&lt;/pre&gt;

&lt;h2&gt;Data Binding&lt;/h2&gt;

&lt;p&gt;
The next step is to bind the employee list to &lt;code&gt;ComboBox&lt;/code&gt; and the &lt;code&gt;DataGrid&lt;/code&gt;. Update the definition to as following:
&lt;/p&gt;

&lt;pre&gt;&amp;lt;mx:ComboBox x=&amp;quot;21&amp;quot; y=&amp;quot;90&amp;quot; &lt;b&gt;dataProvider=&amp;quot;{employees}&amp;quot;&lt;/b&gt;&amp;gt;&amp;lt;/mx:ComboBox&amp;gt;
&amp;lt;mx:DataGrid x=&amp;quot;230&amp;quot; y=&amp;quot;92&amp;quot; width=&amp;quot;302&amp;quot; &lt;b&gt;dataProvider=&amp;quot;{employees}&amp;quot;&lt;/b&gt;&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
Note the special syntax - the variable &lt;code&gt;employees&lt;/code&gt; has been enclosed in the curly-braces &lt;code&gt;{}&lt;/code&gt;. This the data-binding syntax. It is a two step process:
&lt;/p&gt;&lt;ol&gt;
 &lt;li&gt;Declare a variable with the attribute &lt;code&gt;Bindable&lt;/code&gt;.&lt;/li&gt;
 &lt;li&gt;In MXML, use it within the curly braces.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;

&lt;/p&gt;&lt;p&gt;
After binding, the UI should appear as given below:
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/flex/30.Flex.Data-Driven.02.After.Binding.jpg&quot; alt=&quot;Eduzine - Edujini Labs Employees - Data Binding Flex - UI&quot; title=&quot;Eduzine - Edujini Labs Employees - Data Binding Flex - UI&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;&lt;code&gt;ComboBox&lt;/code&gt; Configuration&lt;/h2&gt;

&lt;p&gt;
Everything looks fine except for that the &lt;code&gt;ComboBox&lt;/code&gt; does not exactly display what we require. It displays the &lt;code&gt;String&lt;/code&gt; representation of the &lt;code&gt;UserProfile&lt;/code&gt; using the &lt;code&gt;toString()&lt;/code&gt; method.
&lt;/p&gt;

&lt;p&gt;
All we need to configure is the &lt;code&gt;labelField&lt;/code&gt; of the &lt;code&gt;ComboBox&lt;/code&gt; to tell it that it must pickup the text to be displayed to the UI from the corresponding property / field of the individual object of the collection. The default value of &lt;code&gt;labelField&lt;/code&gt; is &lt;code&gt;label&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
Update the definition of the &lt;code&gt;ComboBox&lt;/code&gt; used to as follows:
&lt;/p&gt;

&lt;pre&gt;  &amp;lt;mx:ComboBox x=&amp;quot;21&amp;quot; y=&amp;quot;90&amp;quot; dataProvider=&amp;quot;{employees}&amp;quot;
       labelField=&amp;quot;employeeName&amp;quot;&amp;gt;
  &amp;lt;/mx:ComboBox&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
Voila! The UI is exactly as what we want:
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/flex/30.Flex.Data-Driven.03.Custom.LabelField.jpg&quot; alt=&quot;Flex ComboBox Data Binding with custom LabelField&quot; title=&quot;Flex ComboBox Data Binding with custom LabelField&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;
In this article, we learnt to:
&lt;/p&gt;&lt;ol&gt;
 &lt;li&gt;Use data binding in Adobe Flex.&lt;/li&gt;
 &lt;li&gt;Bind data to the Flex controls &lt;code&gt;ComboBox&lt;/code&gt; and &lt;code&gt;DataGrid&lt;/code&gt;.&lt;/li&gt;
 &lt;li&gt;Display custom objects in the Flex control &lt;code&gt;ComboBox&lt;/code&gt;.&lt;/li&gt;
 &lt;li&gt;Display custom objects in the Flex control &lt;code&gt;DataGrid&lt;/code&gt; in multiple columns.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;

&lt;/p&gt;    </content:encoded>
    <pubDate>Sat, 07 Jun 2008 22:39:44 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/30-guid.html</guid>
    </item>
</channel>
</rss>
