Call me a nerd, but I do have a favorite ExtJS class: Ext.data.JsonStore(). I like this class for a number of reasons. . . for starters, it saves several lines of code as you don’t have to declare an Ext.data.Store(), Ext.data.HttpProxy() and Ext.data.JsonReader() class just to read data from the server. Ext.data.JsonStore() does all of that for you, making your code cleaner. . . and who doesn’t like clean code?
I recently found one more reason to love this class. If you return a JSON response from the server which contains a “metaData” property (in addition to whatever “root” property mapped to the actual data), you can save yourself even more lines of code!
For example, a traditional JsonStore() declaration and the JSON response it reads:


As you can see, I have declared a variety of config options for my JsonStore() object. The “baseParams” are sent to the “url” specified. The server response should contain a root attribute (in this case “rootAttribute”), and I’ve told the store to look for the fields “id” and “text” within the returned data.
While this is an improvement over declaring a standard Ext.data.Store() with the HttpProxy() and JsonReader() classes, we can further reduce the number of JavaScript lines required to read the data. Assuming you have thousands of lines of code where you utilize many JsonStore() objects, saving 3 or 4 lines of code per object instance results in maybe 100 lines of code saved or more throughout a large application. Furthermore, it allows the JsonStore() class to become much more flexible.
To accomplish this, we’ll add about 3 lines of code to our server-side file (plus or minus any additional logic needed) which returns the JSON data.
Obviously the “root”, “sortInfo” and “fields” attributes of our original config object are necessary. How else would the JsonStore() class know how to read the returned data? By moving this information into another attribute of the returned JSON object, we can dynamically instruct the JsonStore() class to look for the data we want.
If you read the documentation for the Ext.data.JsonReader() class, you’ll find a section near the top discussing the “metaData” attribute. In essence, the data contained within the “metaData” attribute tell the JsonStore() class what to do with the data it just received. In our case, we’ll tell it which attribute is considered the “root” (to which the actual JSON data is mapped), how to sort the data (via “sortInfo”), and which data fields we want to use (via the “fields” array). All of this is configured on the server – meaning that it’s less code to maintain on the client side.
Take a look at the resulting JsonStore() declaration and the JSON data it accepts:


It’s much simpler!
This also means that for classes like Ext.grid.GridPanel() (as well as any other classes which take a “store” config option), we can dynamically change how those classes are used. In the case of a Grid(), we can dynamically change the column model. In other cases, we could reuse the same JsonStore() to load data for different purposes at different times, saving client-side memory and speeding-up the initial page load. I imagine there are a ton of other uses as well.
While a few of the more advanced users of ExtJS might laugh at the fact I’ve just started playing with this, I find it to be immensely helpful! So helpful, in fact, that I wonder why Ext developers aren’t encouraged to make use of it more often. In any case, have fun with this!
I did notice that if your Grid() objects use paging, you must also specify the “totalProperty” attribute in the metaData EVEN IF you had specified it in the config options for the JsonStore() class. Otherwise the paging toolbar fails to recognize that there are more records than listed on the first page.
I’m not entirely sure why, but the data store will ignore your config option.
Hi. Can you help me?? I need load data from JsonStore to TextFied by I don“t know.
I’ve a JsonStore as with
var materiaStore = new Ext.data.JsonStore({
autoLoad: true, //autoload the data
url: ‘fGestionPlanes.php’, //this return a json as [{"mat":"1","mat_nombre":"REDES","mat_ciclo":"9"}]
method: ‘POST’,
totalProperty: ‘totalRows’,
root: ‘data’,
fields: ['mat','mat_nombre','mat_ciclo'],
listeners: {
load: {
fn: function(store, records, options){
var _m=Ext.getCmp(‘mytextfield’);
_m.setValue(store.getAt(0).data.mat_conocimiento_prev);
});
but no liad the data in the textField
Hey Luis,
It doesn’t look like your JSON (returned from the server) has a root element. It should look something like:
{ “data”: [ {"mat":"1","mat_nombre":"REDES","mat_ciclo":"9"} ] }
Give that a shot and let me know if you still need help.
You should also try posting in the ExtJS forums… you’re more likely to get a fast response!
Hey guys, am a big fan of asp.net and working with it for the past few years.
and now I wanna use extjs with asp.net. I wanted to load data from the server to JsonStore but its giving me a headache. In JQuery I know I can pass the url + some method to call on the server but I dont know how to do it in extjs
thanks for your time inadvance
Binalf