PDA

View Full Version : Json store: Handling success. (extjs4, designer)



tfd
19 Jan 2012, 4:30 AM
Hi all. Just a basic and quick question - please do not assume any knowledge from my side - still learning...

I receive data via Json Reader that includes the success value. On server side, I set this success value for every dataset returned based on whether the session is still valid or not. The idea is that, should a session time out, or external access be attempted, success=false will be returned and client side will be redirected to a logged out page.

I've written a function to handle the response, but how do I link that response to the event - and do the same for all the stores? All the examples available assume you can add events directly on the definition, but this is not the case when using the designer to create forms - it replaces all ui scripts.

So where and how do I link this listener to my function. I know its very basic question, but I just cant find usefull examples / docs. None of the 3.x examples seem to be relevant anymore.

tvanzoelen
19 Jan 2012, 4:35 AM
I do it on the callback of the load function of the store. Or else you could try the load event that is triggered after loading the store. You get the success param there.

load( Ext.data.Store this, Ext.data.Model[] records, Boolean successful, Object eOpts )

tfd
19 Jan 2012, 4:50 AM
I do it on the callback of the load function of the store. Or else you could try the load event that is triggered after loading the store. You get the success param there.

load( Ext.data.Store this, Ext.data.Model[] records, Boolean successful, Object eOpts )

Thank you that sounds very usefull... except, as a beginner, I have no idea what you are talking about.

The store is on autoload and there are no events. It must be created, but HOW and WHERE? Keeping in mind that the designer wipes the ui files, they can not be used, so I assume it must be done in eg MyViewPort.js under the apps folder.

Please give a 2 or 3 line example of 1) how I access the specific datastore, not another and 2) how I attach an event to this existing store. It seems I cant use getCmp to find it. So HOW?

tvanzoelen
19 Jan 2012, 4:55 AM
I don't know how to work with the designer.

But in code it is this....



store.on('load', function(store, records, successful){

if(successful == false){
//trigger your redirect function
}

});


Isn't it possible to set an event on the store in the designer?

tfd
19 Jan 2012, 5:04 AM
Doesn't look like it. You can basically only set a few basic properties. And it looks like none of the examples available address this. Took me 2 days to find out where to create the onclick event for buttons.

So where in your example, you have a variable called store that (i assume) points to a specific store, I do not have it. So somehow I need to point that store variable to a specific store.

With buttons, I had to do


Ext.getCmp('logoutBtn').on('click', this.onLogoutClick, this);

in the initComponent of the Viewport that contains the button.

But this does not work with stores.

tvanzoelen
19 Jan 2012, 5:17 AM
I really dont know how to work with the designer. But if you can code somewhere in the viewport,
you can look there in the store manager.

See
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.StoreManager

Do you have a grid or something? What is the reader used for?

tfd
19 Jan 2012, 5:36 AM
Thank you! That page showed me something usefull, but if I stumbled upon it, I really wouldn't have known what I was looking for. The StoreManager can return the store.

The solution seems to be this:



var store = Ext.data.StoreManager.lookup('MyJsonStore');
store.on('load', function(store, records, successful){

if(successful == false)
{
//trigger your redirect function
alert('Data oops!');
}
else
{
alert('Data nice!');
}
});



I do this in the initComponent of my first viewport and then hope for the best.


Re the use: I am busy evaluating the products to make a recommendation to management as to whether this is a suitable road for our new project or not. So trying to figure out the basics to reduce the learning curve for the other devs if we need to go this route. What currently worries me, is the breakages between 3.x 4.0 and 4.1. It looks like there is a lack of consistancy, which would make it hard to recommend. Difficult to tell management: Everytime we upgrade to a new extjs, half of the project will fall over because something like autorefs disappeared in the mist, but is still refered to in the docs and training materials. Just a thought...