PDA

View Full Version : How to handle session



zaps
21 Apr 2007, 2:43 AM
Dear All,

am using ext 1.0 with asp.net
how to Handle session ? while using grid

brian.moeskau
21 Apr 2007, 9:19 AM
Server-side session doesn't have anything to do with Ext or the grid. What exactly are you trying to accomplish?

Animal
21 Apr 2007, 9:45 AM
What I do, is in my login page (which the servlet container forwards to if it recieves a request when the session has expired):



if ("XmlHttpRequest".equals(request.getParameter("X-Requested-With"))) {
session.setAttribute("session-timeout", true);
out.write("<script>top.location.href='" + request.getServletContext() + "';</script>");
return;
}


And as soon as any Element is updated (I always use scripts:true), the browser requests the application's welcome page. The servlet doggedly forwards to the login page. This time with no X-Requested-With header, so it runs as normal.

it just looks for the session attribute "session-timeout" and displays an explanatory message as to why the user seemed to be tipped out of his page.

zaps
21 Apr 2007, 8:25 PM
Animal i want to do same thing in asp.net,
i am using http handler to load data in ext grid.
after session expires,if user do paging grid not refresh,so
i want to redirect to the login page,or i want to create Ext dailog.
to do all this things i have to know that session expired.

Animal
21 Apr 2007, 9:03 PM
Yes, that is a problem with Ajax type apps which pull data in.

I'm wrestling with this too, and I think it is something frameworks like Ext will have to add a scheme to deal with. This will have to be in conjunction with a scheme on the server though.

At a first stab at a plan. Perhaps the server could add some kind of header to the HTTP response to indicate that the session that sent the request is no longer valid, and has no right to any information from the server.

Then the Ext component that handles HTTP requests. HttpProxy or Ext.Connection would have to be configured with info about the header to look for, and thepage to redirect top.location.href to if that header is found.

Perhaps the Ext.Connection class could expose events to which an app could subscribe?

zaps
22 Apr 2007, 12:17 AM
Thanks for ur response animal
You mean say that there is no solution for this?

papasi
22 Apr 2007, 12:50 PM
if you are using json for your grid, check here

http://extjs.com/forum/showthread.php?t=4448

Animal
23 Apr 2007, 12:56 AM
OK, following on, here's the Ext.data.DataProxy class which provides an on method which allows implementations to fire events on the class as well as the instance.

This enables developers to add a central listener for events on the class:



Ext.data.HttpProxy.on("loadexception", function() {
// handle load exception for ALL Proxy instances!
});


I think class level listeners are a good idea. Could this facility be added to Observable, so that all classes which extend Observable get a class level on method?



/**
* @class Ext.data.DataProxy
* This class is an abstract base class for implementations which provide retrieval of
* unformatted data objects.
* <p>
* DataProxy implementations are usually used in conjunction with an implementation of Ext.data.DataReader
* (of the approriate type which knows how to parse the data object) to provide a block of Records
* to an Ext.data.Store.
* <p>
* Custom implementations must implement the load method as described in
* {@link Ext.data.HttpProxy#load}.
*/
Ext.data.DataProxy = function(){
this.events = {
/**
* @event beforeload
* Fires before a network request is made to retrieve a data object.
* @param {Object} params The params parameter to the load function.
*/
beforeload : true,
/**
* @event load
* Fires before the load method's callback is called.
* @param {Object} o The data object.
* @param {Object} arg The callback argument object passed to the load function.
*/
load : true,
/**
* @event loadexception
* Fires if an Exception occurs during data retrieval.
* @param {Object} o The data object.
* @param {Object} arg The callback argument object passed to the load function.
* @param {Object} e The Exception.
*/
loadexception : true
};
Ext.data.DataProxy.superclass.constructor.call(this);
};

Ext.extend(Ext.data.DataProxy, Ext.util.Observable, {
classObservable: function() {
var c = function(){
c.superclass.constructor.apply(this, arguments);
};
Ext.extend(c, Ext.util.Observable, {
events: {
/**
* @event beforeload
* Fires before a network request is made to retrieve a data object.
* @param {Object} params The params parameter to the load function.
*/
beforeload : true,
/**
* @event load
* Fires before the load method's callback is called.
* @param {Object} o The data object.
* @param {Object} arg The callback argument object passed to the load function.
*/
load : true,
/**
* @event loadexception
* Fires if an Exception occurs during data retrieval.
* @param {Object} o The data object.
* @param {Object} arg The callback argument object passed to the load function.
* @param {Object} e The Exception.
*/
loadexception : true
}
});
var classObservable = new c();
Ext.data.DataProxy.on = classObservable.on.createDelegate(classObservable);
return classObservable;
}(),

fireEvent: function() {
return Ext.util.Observable.prototype.fireEvent.apply(this.classObservable, arguments) &&
Ext.util.Observable.prototype.fireEvent.apply(this, arguments);
}
});

tab
23 Apr 2007, 5:47 AM
In the other thread Jack's idea was to do this

ds.on('loadexception', function(a,conn,resp) {
if (resp.status == '304') {
//Ext.Msg.alert('Content has not changed');
} else if (resp.status == '401') {
//Ext.Msg.alert('Authentication required - You need to Login');
}
});

There was some further discussion to the effect that if you wanted pass some different type of JSON data the JSONReader for the ds would not understand it.

It seems like some facility that would redirect the json data would be useful. So when status was 401, this http response would be passed to the another datastore (dslogin) instead. And if someones shopping cart status was changed resulting in status='480' with JSON information about the shopping cart item, then dsshopping could be called?

Is there a way to do this with the DataProxy class?

Animal
23 Apr 2007, 5:57 AM
Yes, that will work for one single Store.

The code I posted will work for all HttpProxies because it makes a class observable.

You could make the Store class observable, but I went to the lower level of the DataProxy.

In fact, the Ext.lib.Ajax class should be made Observable, and offer "success" and "failure" events, so that HTTP statuses and headers can be examined in one call.

zaps
23 Apr 2007, 8:17 PM
Animal shall i change the class file ?

Animal
23 Apr 2007, 11:26 PM
To use that DataProxy which offers a class-level on() method, you'll have to have an overrides file.

I have an overrides file in which I keep my overrides of Ext classes and methods. This is loaded after ext-all.js, and uses the Ext.override method to put my own methods into Ext class prorotypes.

What you need to do is put that code from above, my new DataProxy into the overrides file, and the code of Ext.data.HttpProxy from the Ext src/data directory after it.

You need to do that to redefine HttpProxy to extend the new version of DataProxy.

Then, you should be able to put



Ext.data.HttpProxy.on("loadexception", function(options, response, e) {
// do stuff for all load failures
});
Ext.data.HttpProxy.on("load", function(options, arg) {
// do stuff for all load successes
});


In your page initialization code.

From then on, all HttpProxy objects used by any widgets in the page will fire that handler if they get load exceptions.

As usual, YMMV, you are free to take this idea and run with it, I'[m just getting you started.

zaps
24 Apr 2007, 3:44 AM
Dear Animal

Your idea is excellent, to fire exception for all data proxies.

but the problem is i don't know how to implement override in javascript.:((

could you please write a js file by that i can simply include that in my project.

Thanks in advance

Animal
24 Apr 2007, 3:53 AM
I told you exactly what to do. Put that code in a javascript file and include it after ext-all.js.

In javascript you can define things over and over.

So ext-all.js has



Ext.data.HttpProxy = blah blah...


But later in your overrides.js you have



Ext.data.HttpProxy = my blah blah



That's it, it's overridden.

zaps
24 Apr 2007, 8:30 PM
It sounds clear , Thank you very much Animal

I appreciate your work,its really amazing Thanks again~o)

Animal
24 Apr 2007, 11:25 PM
Post your progress here, I'm interested to know if my theory really works. It's all very well in my head, but reality has a way of differing.

tab
25 Apr 2007, 11:46 AM
You could have overridden HttpProxy instead of DataProxy. The benefit of how you approached it was that any other classes derived from DataProxy (like MemoryProxy) get the same enhancement. Is that the only reason or did I miss something.

This is a very helpful discussion by the way (at least for me :-)

Animal
25 Apr 2007, 11:37 PM
Exactly. Any Proxy classes defined AFTER that new definition of DataProxy will inherit this behaviour.

vadchen
16 Jan 2008, 7:16 AM
Isn't it better using handlers for "requestcomplete" and "requestexception" of Ext.Ajax ?



Ext.Ajax.on('requestexception', handler);


Since any XHR call (including HttpProxy.load) uses Ajax.request, and Ajax is singleton, any server request will fire requestcomplete or requestexception.
Thus, you need only one line and no need to override anything. ~o)

Do I get it right? :-?