home mail me! syndication

overthinkings

by Ruben Daniels

Javeline Platform 101

State management

In traditional web development all logic runs on the server which handles all state. The server shows the page you clicked on, displays the right item in the menu. Context based content will be displayed in certain area’s. Certain buttons will be available others are not. After compiling the entire state of the webapp in a single HTML page it’s send to the browser where it’s displayed.

With Ajax we use a single page interface. There is no refresh. Thus all the above described state management has to be handled within the browser. Javeline Platform offers clean js API’s and an event system. We could use them to set the states of the components in the GUI. I actually did that for a couple of project. After a while I felt it became tedious to switch to JavaScript for this logic. We came up with a system that can do most of this processing automatically by just specifying what you want the state to be. Here is the first example:

<j:Frame caption="Example 1">
   <j:Radiobutton id="rbPage" value="0">
      Page 1
   </j:Radiobutton>
   <j:Radiobutton id="rbPage" value="1" checked="true">
      Page 2
   </j:Radiobutton>
   <j:Radiobutton id="rbPage" value="2">
      Page 3
   </j:Radiobutton>

   <j:Tab id="tbExample" activepage="[rbPage.value]">
      <j:Page caption="Page 0" />
      <j:Page caption="Page 1" />
      <j:Page caption="Page 2" />
   </j:Tab>

   <j:Button id="btnBlah"
      tooltip="[rbPage.value]"
      onclick="alert(this.tooltip)"
      caption="{'Value=' + rbPage.value}" />

   <j:Textbox value="[rbPage.value]" />
</j:Frame>

Please note that I combined this example with the two below.

You can click around the tab and radiobuttons and change the number in the Textbox. Notice that the state of the components are interconnected. Lets take a look at the code. The radiobuttons look normal. We see each has a value ranging from 0 to 2. The middle one is checked. The first dynamic state is set on the activepage attribute for tbExample. It uses a state binding syntax: [rbPage.value]. The brackets tell Platform to create a two-way bind on that property; whenever the value of rbPage changes the activepage property of tbExample will change and vice verse. In the same way the tooltip property of the button is bound.

The other state syntax is used on the caption property of the button. The curly braces tell Platform to create a one-way bind. The curly braces allow javascript code. In this case the caption of the button is set by combining a string ‘Value=’ and the value of rbPage. More advanced javascript such as function calls is possible as well.

In both cases Platform will take care of setting all the necesary events and makes sure the states of the components are always in sync.

I believe in all projects I’ve done, it’s the visibility property that is used most often in state management. Especially in webapps that look like web sites. See the example below:

<j:Frame caption="Example 2 (Hint: Set Tab to 0)">
   <j:Radiobutton id="rbShow" value="true">
      true
   </j:Radiobutton>
   <j:Radiobutton id="rbShow"
      value="false"
      checked="true">
      false
   </j:Radiobutton>

   <j:Label id="lblShow"
      visible="[rbShow.value]"
      height="20">
      Radio is true
   </j:Label>

   <j:Label id="lblAdv"
      visible="{rbShow.value=='true' and rbPage.value==0}"
      width="200">
      Tab 0 is active and radio is true
   </j:Label>
</j:Frame>


Please be especially aware of this rule: {rbShow.value==’true’ and rbPage.value==0}. This is not entirely JavaScript. JavaScript uses && and || in conditional statements. The problem with && in XML is that you have to encode it so the line would become: {rbShow.value==’true’ &amp;&amp; rbPage.value==0}. To prevent this use ‘and’ and ‘or’ in these conditional statements.

In certain cases you want to set the state of multiple components at the same time. This ’state of the application’ can be defined using a j:State component. By specifying a group you can toggle between two or more states:

<j:Frame caption="Example 3">
   <j:State id="myState1"
      value="false"
      group="group1"
      xvalue="true"
      btnTest.caption="First State" />
   <j:State id="myState2"
      value="false"
      group="group1"
      xvalue="false"
      btnTest.caption="Second State" />

   <a href="javascript:group1.toggle()">Toggle States</a>

   <j:Checkbox id="cbTest"
      value="{group1.xvalue}"
      values="true|false"
      height="20" width="200">
      Choose a state using the 2 links
   </j:Checkbox>

   <j:Button id="btnTest" caption="Start State"/>
</j:Frame>


Pages: 1 2 3 4 5 6 7 8 9 10 11

11 Comments »

  Kai Tischler wrote @ October 4th, 2007 at 12:12 am

Hello Ruben !

I have already sent two emails to the Javeline team via the “info@javeline.nl” email
address, but unfortunately I got no response :-(

So I am contacting You directly here via this blog entry; because I have fallen in love with Your layout engine almost immediately :-) ! I am currently doing some research with regard to layout in the native contemporary browsers, and Your approach seems to be really unique ! I have played around with it a little bit and am so impressed right now that I want to leverage Your layout engine ! I know that I can use the packager to extract only parts out of all the Javeline SDK stuff; but I couldn’t make it extract the layout parts … in fact I couldn’t get it to work at all …

I am also very much excited about Javeline in its whole glory, but this particular blog comment shall address the issue of leveraging Your layout engine separately. Is there a technical way to do it ? And what about licensing then ? Or do You know of other layout engines for browsers which are as capable as Yours seems to be ?

I am looking forward to the comments to come !

Cheers

Kai

  Ruben wrote @ October 4th, 2007 at 9:45 am

Hi Kai,

I just found your e-mail. It got lost in the spam box somehow. You will get a reply from me on that. It is possible to extract the layout engine using the packager. For what purpose would you like to use it? Please register to the internals mailinglist at http://developer.javeline.net/mailinglists.php and sent your answers there so the community can follow them as well.

Kind Regards,

Ruben

  overthinkings » Contextmagic and Repeat wrote @ October 22nd, 2007 at 2:49 pm

[…] this was the first time you read about Javeline PlatForm, this article is a good place to start learning more about this Web Application […]

  Mehmet KURT wrote @ January 2nd, 2008 at 4:27 pm

Please ! Help Me…

Using for Framework Javeline… Help Me??

  bharath wrote @ February 9th, 2008 at 3:25 pm

hi , my name is bharath
i have a doubt in ajax connectivity

how to connect an ajax application to database using jdbc…
can u plz clarify my doubt.. with sample example with coding.
thanx & regards.

  Ruben wrote @ February 14th, 2008 at 8:32 am

Hi Bharath,

Ajax applications connect to a backend. Most popular nowadays is to use REST to connect to a backend. This backend can be written in PHP, Perl, ASP, JSP or any other language. It’s the responsibility of that language to connect to a database using jdbc.

In Javeline PlatForm you would do something like this:

<j:teleport>
   <j:rpc id="comm" protocol="POST">
      <j:method name="getUsers" url="http://example.com/users.php">
         <j:variable name="group_id" />
      </j:method>
   </j:rpc>
</j:teleport>

<j:model id="myUsers" load="rpc:comm.getUsers(10)" />

<j:list model="myUsers">
   <j:bindings>
     <j:caption select="@username" />
     <j:icon select="@icon" />
     <j:traverse select="user" />
   </j:bindings>
</j:list>

or if you want to use the javascript approach with Javeline PlatForm you’d do:


var http = new jpf.http();
var data = http.getXml(”http://example.com/users.php?group_id=10″, function(data, state, extra){
   if(state == __HTTP_SUCCESS__){
      alert(”My XML: ” + data.xml);
   }else{
      alert(”An error has occurred”);
   }
});

Please note that these code examples are for v0.9.8 of Javeline PlatForm which will be released shortly.

Kind Regards,

Ruben

[…] early version of Javeline PlatForm and up untill that moment I had only positioned elements using anchoring. I did remember a nice feature that Visual Basic had, where I could align an element to one of the […]

[…] supports Anchoring, Alignment and position using a grid (similar to a table in […]

  Michal wrote @ January 29th, 2009 at 4:19 pm

Hi There,

Guys, I have gone thru the documentation available on javeline.com website nevertheless I am still unable to start with it. There are several examples which I have browse as well but would you have something like tutorial or learn by example so, I can quickly adopt and use your framework?

Thank you,
Michal

  Martijn wrote @ July 6th, 2009 at 1:14 pm

I’m trying to send a form in JPF. I use a set of form elements checkbox, textbox, etc. I would like to send all these elements with Teleport to a php-script to put the values in a database. I get it to work if I declare the variables within the tags. This seems not very efficient to me.

Is it possible (like plain HTML), to submit all the form elements in one, without declaring them again within the Teleport-tags?

I.e.:

  Ruben wrote @ July 6th, 2009 at 2:51 pm

Hi Martijn,

Yes of course. There is a demo on this online here:
http://www.ajax.org/#demos/elements.s.submission

It uses a list bound to the data, but I’m sure you can see that it works the same with form elements bound to the xml.

Kind Regards,
Ruben

Your comment

HTML-Tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>