May 13, 2012, 2:20 PM
[CLOSED] When to use BaseControl Render() vs ToScript() for ASMX/ASHX type scenarios
Hi,
For Ext.NET 2, I can see at least the following ways (I am sure there are more) to create a component on the server during an AJAX request and returning it to the client:
Regarding 4), I've only just come across it, so need to look into it further, but it looks to me like a great new feature. After only a brief look, I can see why 4) may need to remain different to the others (i.e. it sends back a config object/JSON rather than a script to execute, so it needs to be explicitly loaded/used by the calling script - probably by Ext JS's own loader by the looks of it).
However, for the first 3, they all return JavaScript to be executed by the Ext.NET client side framework. However, I can't get option 1) to work for ASHX (see example below) but I can't tell if that is a bug or my misunderstanding of some important differences under the hood.
It would be nice and clean to use option 1) in both ASMX and ASHX approaches if possible (I acknowledge that if in ASHX type of scenario, then using new DirectResponse().Return() would be needed because ProcessRequest() doesn't have a return type.)
If 1) not working for ASHX is actually a bug and can be fixed, then I guess the next question would then be which should be considered "best" or "recommended" way, given both .ToScript and .Render are public methods? .Render() looks cleaner, but maybe I have missed scenarios where it cannot be used and .ToScript needs to be used instead?
Examples:
As suggested by Vladimir on this forum post http://forums.ext.net/showthread.php...correct-script it is possible to call .Render() on a component and return new DirectResponse() which looks cleaner than doing return new DirectResponse(component.ToScript());
Example:
So, for an ASHX, I find I have to stick with the way I used to do it in Ext.NET 1: new DirectResponse(myComponent.ToScript()).Return(); Example:
For Ext.NET 2, I can see at least the following ways (I am sure there are more) to create a component on the server during an AJAX request and returning it to the client:
- Using .Render() on a control and just returning a new DirectResponse() (e.g. ASMX)
- Using return new DirectResponse(component.ToScript()); (e.g. ASMX)
- Using return new DirectResponse(component.ToScript()).Return(); (e.g. ASHX)
- ComponentLoader.Render(component) (e.g. Panel Loader handler)
Regarding 4), I've only just come across it, so need to look into it further, but it looks to me like a great new feature. After only a brief look, I can see why 4) may need to remain different to the others (i.e. it sends back a config object/JSON rather than a script to execute, so it needs to be explicitly loaded/used by the calling script - probably by Ext JS's own loader by the looks of it).
However, for the first 3, they all return JavaScript to be executed by the Ext.NET client side framework. However, I can't get option 1) to work for ASHX (see example below) but I can't tell if that is a bug or my misunderstanding of some important differences under the hood.
It would be nice and clean to use option 1) in both ASMX and ASHX approaches if possible (I acknowledge that if in ASHX type of scenario, then using new DirectResponse().Return() would be needed because ProcessRequest() doesn't have a return type.)
If 1) not working for ASHX is actually a bug and can be fixed, then I guess the next question would then be which should be considered "best" or "recommended" way, given both .ToScript and .Render are public methods? .Render() looks cleaner, but maybe I have missed scenarios where it cannot be used and .ToScript needs to be used instead?
Examples:
As suggested by Vladimir on this forum post http://forums.ext.net/showthread.php...correct-script it is possible to call .Render() on a component and return new DirectResponse() which looks cleaner than doing return new DirectResponse(component.ToScript());
Example:
[WebMethod]
public DirectResponse GetServerTimeWindow()
{
new Window("Server time", Icon.Time)
{
ID = "MyWindow",
Html = DateTime.Now.ToString()
}.Render();
return new DirectResponse();
}
That's nice, and clean. So I tried it for an ASHX. It is virtually the same except because ProcessRequest has no return type, I use new DirectResponse().Return():public void ProcessRequest(HttpContext context)
{
new Window("Server time", Icon.Time)
{
ID = "MyWindow",
Html = DateTime.Now.ToString()
}.Render();
new DirectResponse().Return();
}
But if I do this it doesn't work. Looking at the response in FireBug, the window has come through twice with a < string > in the middle (I added line breaks/formatting to help make it more readable):{
script:"Ext.net.ResourceMgr.destroyCmp(\"App.MyWindow\");
Ext.create(\"Ext.window.Window\",{id:\"MyWindow\",height:100,hidden:false,html:\"13/05/2012 14:33:30\",renderTo:Ext.getBody(),width:200,title:\"Server time\",iconCls:\"#Time\"});
<string>
Ext.net.ResourceMgr.destroyCmp(\"App.MyWindow\");
Ext.create(\"Ext.window.Window\",{id:\"MyWindow\",height:100,hidden:false,html:\"13/05/2012 14:33:30\",renderTo:Ext.getBody(),width:200,title:\"Server time\",iconCls:\"#Time\"});"
}
I tried to follow the code a bit but couldn't tell if that was bug or I have missed a difference about when to use the two different approaches because the first bit in the JavaScript above looks like what I'd expect, while the second bit looks like the start of what I'd expect if it were an ASMX returning the response in XML format...So, for an ASHX, I find I have to stick with the way I used to do it in Ext.NET 1: new DirectResponse(myComponent.ToScript()).Return(); Example:
public class ServerTimeHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var window = new Window("Server time", Icon.Time)
{
ID = "MyWindow",
Html = DateTime.Now.ToString()
};
new DirectResponse(window.ToScript()).Return();
}
public bool IsReusable
{
get { return false; }
}
}
Last edited by Daniil; May 15, 2012 at 10:25 AM.
Reason: [CLOSED]