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

<channel>
	<title>Ed Spencer &#187; browsers</title>
	<atom:link href="http://edspencer.net/tag/browsers/feed" rel="self" type="application/rss+xml" />
	<link>http://edspencer.net</link>
	<description>A Sencha Architect</description>
	<lastBuildDate>Sat, 11 Feb 2012 09:20:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Ext.History</title>
		<link>http://edspencer.net/2009/01/using-exthistory.html</link>
		<comments>http://edspencer.net/2009/01/using-exthistory.html#comments</comments>
		<pubDate>Fri, 09 Jan 2009 11:17:00 +0000</pubDate>
		<dc:creator>Ed Spencer</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[extjs]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[history]]></category>

		<guid isPermaLink="false">http://192.168.2.15/Projects/wordpress/?p=19</guid>
		<description><![CDATA[<p><a href="http://extjs.com/deploy/dev/docs/?class=Ext.History">Ext.History</a> is a small class that was released with ExtJS 2.2, making it easy to use the browser&#8217;s back and forward buttons without breaking your AJAX-only pages.</p>
<p>This can be really useful for any ExtJS application with more than one view, for example a simple app with a grid of Products, which can be double-clicked to reveal an edit form.  Ext.History allows the user to click the back button to go back to the grid if they&#8217;re on the form, and even forward again from the grid.  It does this by appending a token to the end of the url:</p>
<pre class="brush: xml;">http://myurl.com/ (default url for the app)
http://myurl.com/#products (shows the products grid)
http://myurl.com/#products/edit/1 (shows the edit form for product 1)
</pre>
<p>This is useful, so let&#8217;s look at how to set it up.  Ext.History requires that a form field and an iframe are present in the document, such as this:</p>
<pre class="brush: xml;">
&lt;form id=&quot;history-form&quot; class=&quot;x-hidden&quot; action=&quot;#&quot;&gt;
  &lt;div&gt;
    &lt;input id=&quot;x-history-field&quot; type=&quot;hidden&quot; /&gt;
    &lt;iframe id=&quot;x-history-frame&quot;&gt;&lt;/iframe&gt;
  &lt;/div&gt;
&lt;/form&gt;
</pre>
<p>The div is just there to make the markup valid.  Ext.History uses the iframe to make IE play nice.  Generally I don&#8217;t like to make any assumptions about what is in the DOM structure so I use Ext to generate these elements:</p>
<pre class="brush: jscript;">
/**
* Creates the necessary DOM elements required for Ext.History to manage state
* Sets up a listener on Ext.History's change event to fire this.handleHistoryChange
*/
initialiseHistory: function() {
  this.historyForm = Ext.getBody().createChild({
    tag:    'form',
    action: '#',
    cls:    'x-hidden',
    id:     'history-form',
    children: [
      {
        tag: 'div',
        children: [
          {
            tag:  'input',
            id:   Ext.History.fieldId,
            type: 'hidden'
          },
          {
            tag:  'iframe',
            id:   Ext.History.iframeId
          }
        ]
      }
    ]
  });

  //initialize History management
  Ext.History.init();
  Ext.History.on('change', this.handleHistoryChange, this);
}
</pre>
<p>Ext.History.fieldId and Ext.History.iframeId default to &#8216;x-history-field&#8217; and &#8216;x-history-frame&#8217; respectively.  Change them before running initialiseHistory if you need to customise them (Ext.History is just a singleton object so you can call Ext.History.fieldId = &#8217;something-else&#8217;).</p>
<p>The main method you&#8217;ll be using is Ext.History.add(&#8217;someurl&#8217;).  This adds a token to the history stack and effectively redirects the browser to http://myurl.com/#someurl.  To create something like the grid/form example above, you could write something like this:</p>
<pre class="brush: jscript;">
Ext.ns('MyApp');

MyApp.Application = function() {
  this.initialiseHistory();

  this.grid = new Ext.grid.GridPanel({
    //set up the grid...
    store: someProductsStore,
    columns: ['some', 'column', 'headers'],

    //this is the important bit - redirects when you double click a row
    listeners: {
      'rowdblclick': {
        handler: function(grid, rowIndex) {
          Ext.History.add(&quot;products/edit/&quot; + rowIndex);
        }
      }
    }
  });

  this.form = new Ext.form.FormPanel({
    items: ['some', 'form', 'items'],

    //adds a cancel button which redirects back to the grid
    buttons: [
      {
        text: 'cancel',
        handler: function() {
          Ext.History.add(&quot;products&quot;);
        }
      }
    ]
  });

//any other app startup processing you need to perform
};

MyApp.Application.prototype = {
  initialiseHistory: function() {
    //as above
  },

  /**
   * @param {String} token The url token which has just been navigated to
   * (e.g. if we just went to http://myurl.com/#someurl, token would be 'someurl')
   */
  handleHistoryChange: function(token) {
    var token = token || &quot;&quot;;
    switch(token) {
      case 'products':        this.showProductsGrid();     break;
      case 'products/edit/1': this.showProductEditForm(1); break;
      case '':                //nothing after the #, show a default view
    }
  },

  showProductsGrid: function() {
    //some logic to display the grid, depending on how your app is structured
  },

  showProductEditForm: function(product_id) {
    //displays the product edit form for the given product ID.
  }
};

Ext.onReady(function() {
  var app = new MyApp.Application();
});
</pre>
<p>So when you visit http://myurl.com/#products, showProductsGrid() will be called automatically, and when you visit http://myurl.com/#products/edit/1, showProductEditForm() will be called with the argument 1.  You can write your own logic here to change tab or show a window or whatever it is you do to show a different view to the user.</p>
<p>I&#8217;m not suggesting you parse the url token using a giant switch statement like I have above &#8211; this is only an example.  You could get away with something like that for a very small app but for anything bigger you&#8217;ll probably want some kind of a router.  That goes a little beyond the scope of this article but it is something I will return to at a later date.</p>
<p>There is also an example of Ext.History available on the <a href="http://extjs.com/deploy/dev/examples/history/history.html">Ext samples pages</a>.</p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://extjs.com/deploy/dev/docs/?class=Ext.History">Ext.History</a> is a small class that was released with ExtJS 2.2, making it easy to use the browser&#8217;s back and forward buttons without breaking your AJAX-only pages.</p>
<p>This can be really useful for any ExtJS application with more than one view, for example a simple app with a grid of Products, which can be double-clicked to reveal an edit form.  Ext.History allows the user to click the back button to go back to the grid if they&#8217;re on the form, and even forward again from the grid.  It does this by appending a token to the end of the url:</p>
<pre class="brush: xml;">http://myurl.com/ (default url for the app)
http://myurl.com/#products (shows the products grid)
http://myurl.com/#products/edit/1 (shows the edit form for product 1)
</pre>
<p>This is useful, so let&#8217;s look at how to set it up.  Ext.History requires that a form field and an iframe are present in the document, such as this:</p>
<pre class="brush: xml;">
&lt;form id=&quot;history-form&quot; class=&quot;x-hidden&quot; action=&quot;#&quot;&gt;
  &lt;div&gt;
    &lt;input id=&quot;x-history-field&quot; type=&quot;hidden&quot; /&gt;
    &lt;iframe id=&quot;x-history-frame&quot;&gt;&lt;/iframe&gt;
  &lt;/div&gt;
&lt;/form&gt;
</pre>
<p>The div is just there to make the markup valid.  Ext.History uses the iframe to make IE play nice.  Generally I don&#8217;t like to make any assumptions about what is in the DOM structure so I use Ext to generate these elements:</p>
<pre class="brush: jscript;">
/**
* Creates the necessary DOM elements required for Ext.History to manage state
* Sets up a listener on Ext.History's change event to fire this.handleHistoryChange
*/
initialiseHistory: function() {
  this.historyForm = Ext.getBody().createChild({
    tag:    'form',
    action: '#',
    cls:    'x-hidden',
    id:     'history-form',
    children: [
      {
        tag: 'div',
        children: [
          {
            tag:  'input',
            id:   Ext.History.fieldId,
            type: 'hidden'
          },
          {
            tag:  'iframe',
            id:   Ext.History.iframeId
          }
        ]
      }
    ]
  });

  //initialize History management
  Ext.History.init();
  Ext.History.on('change', this.handleHistoryChange, this);
}
</pre>
<p>Ext.History.fieldId and Ext.History.iframeId default to &#8216;x-history-field&#8217; and &#8216;x-history-frame&#8217; respectively.  Change them before running initialiseHistory if you need to customise them (Ext.History is just a singleton object so you can call Ext.History.fieldId = &#8217;something-else&#8217;).</p>
<p>The main method you&#8217;ll be using is Ext.History.add(&#8217;someurl&#8217;).  This adds a token to the history stack and effectively redirects the browser to http://myurl.com/#someurl.  To create something like the grid/form example above, you could write something like this:</p>
<pre class="brush: jscript;">
Ext.ns('MyApp');

MyApp.Application = function() {
  this.initialiseHistory();

  this.grid = new Ext.grid.GridPanel({
    //set up the grid...
    store: someProductsStore,
    columns: ['some', 'column', 'headers'],

    //this is the important bit - redirects when you double click a row
    listeners: {
      'rowdblclick': {
        handler: function(grid, rowIndex) {
          Ext.History.add(&quot;products/edit/&quot; + rowIndex);
        }
      }
    }
  });

  this.form = new Ext.form.FormPanel({
    items: ['some', 'form', 'items'],

    //adds a cancel button which redirects back to the grid
    buttons: [
      {
        text: 'cancel',
        handler: function() {
          Ext.History.add(&quot;products&quot;);
        }
      }
    ]
  });

//any other app startup processing you need to perform
};

MyApp.Application.prototype = {
  initialiseHistory: function() {
    //as above
  },

  /**
   * @param {String} token The url token which has just been navigated to
   * (e.g. if we just went to http://myurl.com/#someurl, token would be 'someurl')
   */
  handleHistoryChange: function(token) {
    var token = token || &quot;&quot;;
    switch(token) {
      case 'products':        this.showProductsGrid();     break;
      case 'products/edit/1': this.showProductEditForm(1); break;
      case '':                //nothing after the #, show a default view
    }
  },

  showProductsGrid: function() {
    //some logic to display the grid, depending on how your app is structured
  },

  showProductEditForm: function(product_id) {
    //displays the product edit form for the given product ID.
  }
};

Ext.onReady(function() {
  var app = new MyApp.Application();
});
</pre>
<p>So when you visit http://myurl.com/#products, showProductsGrid() will be called automatically, and when you visit http://myurl.com/#products/edit/1, showProductEditForm() will be called with the argument 1.  You can write your own logic here to change tab or show a window or whatever it is you do to show a different view to the user.</p>
<p>I&#8217;m not suggesting you parse the url token using a giant switch statement like I have above &#8211; this is only an example.  You could get away with something like that for a very small app but for anything bigger you&#8217;ll probably want some kind of a router.  That goes a little beyond the scope of this article but it is something I will return to at a later date.</p>
<p>There is also an example of Ext.History available on the <a href="http://extjs.com/deploy/dev/examples/history/history.html">Ext samples pages</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://edspencer.net/2009/01/using-exthistory.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

