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.
Hope this help.....!
Comments
It's enormous that you are getting ideas from this paragraph as well as from our argument made at this time. yahoo sign in