home mail me! syndication

overthinkings

of Ruben Daniels

Back button and state solution in Javeline PlatForm v0.98.2

In this article I show you how to control the backbutton in your Javeline PlatForm application. I’ll take you step by step through creating a breadcrumb navigation for a typical product database site. It uses 4 states (similar to pages in static websites). The key to this implementation is that all state information is encoded in the hash. All information is stored in the url using the hash (http://example.com#information). This makes navigating your ajax site as intuitive as any website:

State Description
Home The state where the application starts and can return to start searching
Search The state where the search results are displayed
Product The state where the product presentation is displayed

Why use state objects?

An alternative to using state objects is to define the state in a j:pages object (similar to a tab component but without the buttons). This would allow you to group the components you need in different pages. One page can be showed at a time, showing the underlying components. This limits you significantly in reusing components on different ‘pages’ (states). In the example below, we’ll be creating a ‘trNav’ object that is used both on the home state and the search state, but not the product state. As you can see this allows for a much higher granularity in control and reuse.

Setting up the basic JML

We’ll first set up some components. There will be 3 states represented in the application. In a real application you’d might want to use a completely different way of setting up your UI, but the principle will be the same.

//Create some Javeline Components using some markup
<j:text id="txtBreadcrumb" height="40" width="300"/>
<j:tree id="trNav" height="100"/>
<j:bar id="barHome">
  <h3>Home</h3>
</j:bar>
<j:bar id="barSearch">
  <h3>Search</h3>
  <j:textbox id="inKeyword" value="my_product" />
</j:bar>
<j:bar id="lstProduct"><h3>Product</h3></j:bar>

By setting up 3 state objects we can specify which objects are displayed during which state:

<j:state id="stHome"
  trNav.visible="true"
  barHome.visible="true"
  barSearch.visible="false"
  lstProduct.visible="false" 

  active="true"
  />

<j:state id="stSearch"
  trNav.visible="true"
  barHome.visible="false"
  barSearch.visible="true"
  lstProduct.visible="false" />

<j:state id="stProduct"
  trNav.visible="false"
  barHome.visible="false"
  barSearch.visible="false"
  lstProduct.visible="true" />

Creating the breadcrumb javascript

As you can see we haven’t specified the txtBreadcrumb component, it will therefore be visible throughout the existence of the application. The home state is set to active. This activates the home state right after the application started. Because the breadcrumb navigation is always implementation specific, we’ll write a small javascript object to handle things for us:

CrumbNav = {
  last : [],

  exec : function(type, id){
    switch(type){
      case "home":
        stHome.activate();
      break;
      case "search":
        return doSearch(id || inKeyword.getValue());
      break;
      case "product":
        stProduct.activate();
      break;
    }

    CrumbNav.set(type);
  },

  add : function(type, id){
    //Creates a link tag in HTML
    return '<a href="javascript:void(0)" onclick="CrumbNav.exec('" + type + "', '' + id + '')">" + type.uCaseFirst() + "</a>';
  },

  set : function(type, id){
    var str = [this.add('home')];
    if(type == "home"){
      this.last = [];
      //Join the elements of the array creating a string of all the link tags and passes it to the txtBreadcrumb component.
      txtBreadcrumb.setValue(str.join(" &gt; "));
      return;
    }

    if(type != "search" && this.last.length)
      str.push(this.add(this.last[0], this.last[1]));
    str.push(this.add(type, id));

    this.last = [type, id];
    //Join the elements of the array creating a string of all the link tags and passes it to the txtBreadcrumb component.
    txtBreadcrumb.setValue(str.join(" &gt; "));
  }
}

function doSearch(id){
  //handle searching here
  CrumbNav.set("search", id);
  stSearch.activate();
  inKeyword.setValue(id);
}

We’ll add some action to our application by adding 3 buttons that switch to a state:

<j:button caption="Home" onclick="CrumbNav.exec('home');" />
<j:button caption="Search" onclick="CrumbNav.exec('search');" />
<j:button caption="Product" onclick="CrumbNav.exec('product');" />

If you run the application again you will find yourself in control of the states of the application. You will also see the breadcrumb navigation being created as you press the buttons, one after the other.

The History object

We still need to implement back button support. We will record the state in the hash. The state consists of both the active state and an id containing information about the search or the product that is loaded. To do this we’ll add the following line to the set function.

  jpf.History.addPoint(type + "|" + (id || ""));

Now just add the onchange event to handle page navigation:

jpf.History.onchange = function(page){
  var data = page.split("|");
  CrumbNav.exec(data[0], data[1]);
}

If you now click around the application you will see the backbutton lighting up. You can use it to change the state and to go back whenever you want, and forward to get your state back. Now it’s working all fine, and almost exactly like old-skool non-ajax sites work. But there’s one last thing to do, and that is being able to copy&paste url’s from the title bar to others. Add the following code at the end to set the state at the start of the application. You’ll need to to remove active=”true” from the stHome element for this to work.

var state = location.href.match(/#(.*)$/) ? RegExp.$1 : "home";
jpf.History.init(state);

Conclusion

In this article we have created a native website navigation in an Ajax application using Javeline PlatForm. Although not build for IE8, this solution closely resembles IE8’s way of handling hash changes (with the onhashchange event). When IE8 stops crashing my pc I will make it work natively for IE8. This solution has been tested in Safari 3, Firefox and IE6+.

This works from v0.98.2 which can be downloaded from http://developer.javeline.net/downloads.php. You can find the sample code here.

2 Comments »

[…] refer to each bar component by it’s id. (N.B. You could also use a state component to set the states of multiple components). The process of hiding and showing multiple components has been optimized such that the layout is […]

[…] helps getting control and overview of the different page like states. You might want to read this blog article on state management together with controlling the […]

Your comment

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

cheap propecia pill buying cialis discount cialis buy cialis internet buy generic cialis online cheap accutane no prescription cialis us online propecia cheapest cialis price accutane for sale buy viagra overnight delivery no rx levitra order levitra on internet levitra without rx buy cialis no prescription required cialis no prescription cheapest levitra cialis bangkok buy propecia overnight delivery accutane malaysia buy accutane on internet viagra in malaysia discount propecia no rx discount accutane without prescription discount viagra overnight delivery find discount cialis online cheapest viagra accutane sales propecia malaysia buy no rx cialis free levitra propecia pills low cost viagra online pharmacy viagra find cheap cialis levitra australia levitra in australia overnight levitra viagra free delivery buy cheapest cialis on line cialis buy order cialis without prescription viagra buy accutane sale overnight propecia order levitra without prescription lowest price accutane cheapest cialis levitra uk levitra free delivery no prescription accutane cheap viagra from usa accutane online pharmacy order discount cialis online viagra tablets cost propecia levitra price cheap accutane from canada accutane from canada find accutane online accutane pills lowest price cialis find accutane no prescription required order viagra on internet cheap propecia from canada lowest price for accutane viagra without a prescription cheap cialis from uk buy levitra in canada find viagra overnight accutane cheap levitra from canada cost of propecia cheap cialis internet find propecia online buy levitra generic levitra tablet cialis cheap price accutane vendors cost accutane discount levitra levitra no rx required buy discount cialis online sale cialis cialis cost propecia without rx accutane without a prescription accutane online online pharmacy cialis propecia bangkok levitra no online prescription cheap levitra no prescription buy propecia without prescription order cialis on internet buy cialis in canada buy viagra online accutane pharmacy online compare accutane prices online find propecia on internet propecia pill generic cialis cialis generic buy discount levitra free accutane buy viagra generic cheap cialis pill propecia drug cialis without a prescription purchase cialis without prescription cheap levitra online overnight cialis pharmacy cialis buy accutane from india find cialis online viagra in us order accutane from us viagra discount levitra online cialis cheap drug buy levitra on line discount viagra find no rx propecia accutane order levitra prescription accutane uk cheap accutane on internet compare levitra prices generic viagra online cialis pills cheap cialis in canada no rx propecia approved levitra pharmacy compare cialis prices order cheap accutane online propecia uk buy cheapest accutane on line order levitra no prescription required order propecia from canada buy viagra on internet order discount levitra propecia canada where to order levitra viagra no rx required drug viagra buy levitra from india buy accutane lowest price levitra in malaysia cheapest levitra online viagra cheap drug cheap levitra from usa levitra in uk cheap levitra on internet no prescription propecia order discount levitra online purchase cialis overnight delivery cheap price propecia cialis free delivery cheapest generic viagra cheap cialis no rx find discount levitra online buy no rx levitra find discount propecia generic levitra buy levitra on internet propecia cheap drug cheap levitra no rx accutane medication compare levitra prices online buy propecia buy accutane us order viagra no prescription required find no rx accutane find propecia propecia no rx viagra overnight buy cheap propecia online cialis from canada buy levitra online purchase propecia overnight delivery buy propecia online cheap accutane pill cheap viagra accutane without rx buy viagra without prescription order propecia without prescription buying accutane cheapest propecia price cialis cheapest price levitra overnight shipping viagra online pharmacy pharmacy levitra find discount levitra propecia online pharmacy cheapest generic accutane purchase accutane without prescription cialis no rx propecia side effects cialis buy online discount cialis online accutane in bangkok buy cheap cialis online order generic viagra order propecia from us accutane india buy propecia us tablet propecia find cheap accutane order accutane on internet cheap propecia find discount viagra levitra in us order levitra order discount viagra buying viagra online buy propecia from canada accutane buy drug viagra canada no rx cialis cheap propecia from usa order levitra online buy viagra us certified propecia accutane cheapest price approved accutane pharmacy cheap viagra without prescription buy cheap levitra internet find accutane buy viagra from india buy cheapest levitra on line propecia from canada low cost propecia cheap cialis in uk buy cialis no rx order accutane in us propecia cheapest price cheapest levitra price cheap viagra in canada buying generic viagra buy viagra no rx propecia rx buy cialis from us propecia buy online discount accutane viagra no prescription buy propecia in us buy levitra buy accutane generic purchase viagra without prescription order cheap cialis viagra pills cheap accutane pharmacy buy cialis from canada propecia in malaysia propecia overnight shipping accutane overnight buying cialis online cheap propecia from uk cialis purchase viagra sales cheap accutane in canada buy accutane online cialis no rx required accutane approved propecia us cialis pill propecia india accutane in malaysia cheap price viagra propecia prescription buy accutane on line propecia sales levitra generic accutane for order cheap viagra online order levitra overnight delivery find cheap propecia online buying levitra cialis sales buy generic accutane buy viagra online cheap order cialis no prescription required buying generic cialis viagra drug buy viagra lowest price viagra price approved propecia pharmacy cheap cialis pharmacy cheap viagra no rx find no rx cialis order cheap propecia cheap levitra in usa cialis without prescription buy cheapest propecia on line where to order viagra accutane cost order propecia cheap online accutane drug cheapest cialis online buy generic cialis cialis for order cialis prescription find cheap propecia compare cialis prices online propecia cost cheap cialis tablet purchase levitra discount propecia overnight delivery cialis india viagra australia accutane cheap drug discount viagra online cheap viagra tablet accutane australia propecia in bangkok buy cheap levitra cialis price buy generic levitra online buy cheapest cialis online levitra cost order levitra from canada viagra pharmacy online viagra without prescription viagra in uk find viagra no prescription required buy generic accutane online propecia pharmacy viagra online buy discount levitra online cialis online pharmacy viagra generic buy cialis lowest price cost of cialis find cialis levitra online drug propecia cialis tablet buy cialis low price propecia vendors cheapest generic cialis cheap propecia in usa discount propecia buy generic viagra online buying generic levitra online accutane buy levitra from canada order propecia overnight delivery best price propecia order accutane no rx cialis side effects order viagra without prescription cheap propecia on internet propecia free delivery tablet viagra levitra pills find cheap viagra buy accutane from us viagra medication buying propecia cialis from india buy cheap cialis buy accutane buy cheap levitra online find no rx levitra buy cheapest viagra on line low cost accutane propecia in us order accutane overnight delivery buy levitra lowest price approved cialis pharmacy purchase cialis online discount cialis overnight delivery buy levitra no rx order no rx levitra online pharmacy accutane discount viagra without prescription accutane no rx required cheap accutane without prescription cialis information cialis overnight fda approved cialis propecia without prescription order levitra in canada buy discount propecia fda approved viagra cheap price cialis lowest price for levitra cialis in bangkok purchase cialis discount levitra no rx cheapest generic levitra lowest price for propecia propecia buy drug cheap cialis no prescription cheap viagra from uk no rx accutane buy levitra us certified levitra cialis order generic propecia find cialis no prescription required buy viagra in us buy cialis us certified cialis levitra overnight delivery levitra without prescription buy cialis on internet fda approved levitra cost viagra viagra pill buy discount viagra online cheap levitra tablet best price for propecia buy discount viagra compare viagra prices online generic levitra cheap accutane side effects cialis order find propecia without prescription levitra accutane overnight shipping find cheap accutane online buy generic levitra levitra vendors buy viagra from us buy cheap viagra online order viagra no prescription buy cialis on line propecia online without prescription cheapest generic levitra online levitra from india cost of levitra cheap propecia overnight delivery viagra overnight shipping cheap accutane internet purchase propecia no rx find levitra viagra cost levitra no rx find cheap levitra online accutane price cheap accutane tablet best price cialis order generic levitra best price viagra levitra canada viagra cheapest price drug cialis viagra approved sale viagra cheap accutane no rx cheapest viagra price cheapest accutane generic cialis online cialis no online prescription find cheap viagra online cheap levitra internet buy no rx accutane