Skip to main content

Ext JS 4 - Client Proxies

The web storage concepts in HTML5 are really cool and used for store/read data to/from browser memory. There are two types of web storage concepts available in HTML5.

    1. Session storage - The data only availble for particular session.
    2. Local storage - Long term storage and data is shared by various browser and tabs

The Ext JS gives lot of flexibility for using web storage concepts and it do lot of operations internally and reduced the developer works. The most well known issues is, the HTML5 web storage concepts only allow to store a value like string, int, float and boolean and it don't allow to store a complex objects like JSON. But Ext JS directly allows to store/read JSON data to/from web storage. Internally it performs serialize and deserialize operation for store/read JSON data to/from web storage.

The proxies in Ext JS are used for performing above operation. There are three kind of client side proxies  available in Ext JS.
    
    1. Session storage proxy
    2. Local storage proxy
    3. Memory proxy

These proxies are directly used by either in models or stores. Let's see an example for above proxies.

1. Session storage proxy.


The session storage data is only available for particular session. Once the browser has closed, the data will be lost. I am going to store/read some basic information of the user like user name and display name by using session storage. 

Let's create a simple model for user and we can reuse this model in various stores.

Ext.define('MyApp.model.User',{
 extend : 'Ext.data.Model',
 fields : [
  {name : 'userId',type : 'int'},
  {name : 'userName', type : 'string'},
  {name : 'displayName', type : 'string'}
 ]
});

Let's create a session storage store. Here the proxy type "sessionstorage" indicates, the data is storing into session storage. The id property is a key for storing/reading values to/from session storage.

Ext.define('MyApp.store.SessionStorageUser',{
 extend : 'Ext.data.Store',
 model : 'MyApp.model.User',
 proxy : {
  type : 'sessionstorage',
  id : 'userPreferences'
 }
})

Let's create a session storage controller and will see how to store a value and read a value by using session storage.

Ext.define('MyApp.controller.SessionStorageController',{
 extend : 'Ext.app.Controller',
 models : ['User'],
 stores : ['SessionStorageUser'],
 init : function() {
  //Storing information to session storage
  var sessionStore = this.getStore('SessionStorageUser');
  sessionStore.load();
  sessionStore.add({userId:1,userName : 'suriyan',displayName : 'Suriya'});
  sessionStore.sync();
  
  //Reading information from session storage
  sessionStore.load(function(records,operation,success) {
   //Here records is a array type. For testing purpose, I took first record.
   console.log('User name : ' + records[0].get('userName') + ', display name : ' + records[0].get('displayName'));
  });
  
  console.log('Session storage operation has completed');
 }
});

The result of the above code is as shown below.



2. Local storage proxy.


The local storage data is available for long time (until removing data from local storage memory) and we can share the data between various browser/tabs. 

Let's create a local storage store. Note : the proxy type of the store.

Ext.define('MyApp.store.LocalStorageUser',{
 extend : 'Ext.data.Store',
 model : 'MyApp.model.User',
 proxy : {
  type : 'localstorage',
  id : 'userPreferences'
 }
})

Let's create a local storage controller for testing above store.

Ext.define('MyApp.controller.LocalStorageController',{
 extend : 'Ext.app.Controller',
 model : ['User'],
 stores : ['LocalStorageUser'],
 init : function() {
  //Getting handle of local storage store.
  var localStore = this.getStore('LocalStorageUser');
  
  //Adding information to local storage
  localStore.add({userId:1,userName : 'suriyan',displayName : 'suriya'});
  localStore.sync();
  
  //Reading information from local storage
  localStore.load(function(records,operation,success){
   console.log('User name : ' + records[0].get('userName') + ', display name : ' + records[0].get('displayName'));
  });
  console.log('Local storage operation has completed');
 }
})

The result of the above code is as shown below.



3. Memory proxy.


Memory proxy is very useful whenever you want to store the data temporally. The data will be lost on every page refresh. For an example, in my application I need to display list of countries to users for selection purpose. In this situation we can use our memory proxy concept for displaying country list. In future we want to add some more countries than, there is no need for modifying our existing code.

Le's create a model for country.

Ext.define('MyApp.model.Country',{
 extend : 'Ext.data.Model',
 fields : [
  {name : 'id', type : 'int'},
  {name : 'country', type : 'string'}
 ]
});

Let's create a store for country model, here check the proxy type.

Ext.define('MyApp.store.CountryStore',{
 extend : 'Ext.data.Store',
 model : 'MyApp.model.Country',
 data : countryData,
 proxy : {
  type : 'memory',
  reader : {
   type : 'json',
   root : 'countryList'
  }
 }
});

Let's create a combobox for testing above store. Note: I am following MVC pattern and this is my country view.

Ext.define('MyApp.view.Country',{
 extend : 'Ext.panel.Panel',
 alias : 'widget.country',
 title : 'Memory proxy testing',
 layout : {
  type : 'vbox'
 },
 height : 400,
 width : 400,
 items : [
  {
   xtype : 'combobox',
   store : 'CountryStore',
   fieldLabel : 'Select a country',
   displayField : 'country',
   valueField : 'country',
   queryMode : 'local',
   typeAhed : false
  }
 ]
});

Let's create a controller for memory proxy and in controller init function, render our view into HTML body.

Ext.define('MyApp.controller.MemoryController',{
 extend : 'Ext.app.Controller',
 models : ['Country'],
 stores : ['CountryStore'],
 views : ['Country'],
 init : function() {
  Ext.create("MyApp.view.Country",{
   renderTo : Ext.getBody()
  })
 }
});

Finally, this is my app.js code.

Ext.Loader.setConfig({
 enabled : true
});

var countryData = {
 countryList : [
  {id : 1, country : 'India'},
  {id : 2, country : 'Canada'},
  {id : 3, country : 'Germany'},
  {id : 4, country : 'US'},
  {id : 5, country : 'UK'}
 ]
};

Ext.application({
 name : 'MyApp',
 appFolder : 'app',
 //controllers : ['SessionStorageController'],
 //controllers : ['LocalStorageController'],
 controllers : ['MemoryController'],
 launch : function() {
  console.log('Application launch function...!');
 }
});

The result of the above code is as shown in figure.


The application source code is uploaded into my github repository.

Hope this help.....!

Comments

No doubt your blog article is great to me and most importantly it gives me information about my related issue.
Travis Smith said…

It's enormous that you are getting ideas from this paragraph as well as from our argument made at this time. yahoo sign in

Popular posts from this blog

Ext JS 4 – Creating web page using Ext.container.Viewport class and region propery

The Ext.container.Viewport is used for creating general web page and region property is used for splitting web page into different parts. Ext.container.Viewport is a specialized container represents the viewable area of the application (the browser area). It render itself to the document body, there is no need for providing renderTo property and it automatically sizes itself to size of the browser viewport. The viewport also re-size the child elements based on view area(based on browser width and height). The default layout of the viewport is border layout and we can customize this property according to our requirements. The viewport does not provide any scrolling. If necessary, the child elements(generally panels) within viewport needs to provide a scroll feature by using autoScroll property. See the below example for better understanding. This is Home.js file placed into app/view folder. Ext.define('MyApp.view.Home', { extend : 'Ext.container.Viewport&#

Getting servlet init parameter that is defined in web.xml

1. Create a new dynamic web project using eclipse. 2. Create a one new servlet and include the following code. package com.controller ; import java.io.IOException; import javax.servlet .ServletConfig; import javax.servlet .ServletException; import javax.servlet .annotation.WebServlet; import javax.servlet .http.HttpServlet; import javax.servlet .http.HttpServletRequest; import javax.servlet .http.HttpServletResponse; @ WebServlet ( "/LoginController" ) public class LoginController extends HttpServlet {                 private static final long serialVersionUID = 1L;                 public LoginController() {         super ();     }     protected void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {                                 ServletConfig config = getServletConfig ();                                 System.out.println( "Init parameter user nam

Simple Login Application Using Spring MVC and Hibernate – Part 1

 I hope developers working in web application development might hear about MVC architecture. Almost all technologies provide support for MVC based web application development, but the success is based on many factors like reusable of the code, maintenance of the code, future adaption of the code, etc..,  The success of the Spring MVC is “ Open for extension and closed for modification ” principle. Using Spring MVC the developers can easily develop MVC based web application. We don’t need any steep learning curve at the same time we need to know the basics of spring framework and MVC architecture. The Spring MVC consists of following important components. 1. Dispatcher servlet 2. Controller 3. View Resolver 4. Model Spring MVC - Overview  The overall architecture of Spring MVC is shown here.  1. When “Dispatcher Servlet” gets any request from client, it finds the corresponding mapped controller for the request and just dispatches the request to the corresponding contro