That was great!
But the issue is if you have tons of pages in one assembly that need lots of Ajax function for different purposes, it renders all methods to every page while it might not be needed, so I made some modification (in sample application not Library), hope that helps:
Instead of including ApiHandler.ashx as a script block, I used it as a helper class like this:
Quote:
public static void Register(Page page) {
DirectProviderCache cache = DirectProviderCache.GetInstance();
DirectProvider provider;
string hash = GetHashKey(page);
string key = "Ext.app.REMOTING_API." + hash;
//After being configured, the provider should be cached.
if (!cache.ContainsKey(key)) {
provider = new DirectProvider("Ext.app.REMOTING_API", "/DirectHandler.ashx?hash=" + hash);
provider.Configure(new object[] { page });
cache.Add(key, provider);
}
else {
provider = cache[key];
}
string script = provider.ToString();
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "ApiHandler", script, true);
}
private static string GetHashKey(Object o) {
return o.GetType().GetHashCode().ToString();
}
The every page that needs this functionality has to call:
Quote:
ApiHandler.Register(this);
In the Load event.
As this register every page in different Cache key reference I needed to do an alter in Directhandler as well:
Quote:
public void ProcessRequest(HttpContext context)
{
string key = "Ext.app.REMOTING_API." + context.Request.QueryString["hash"];
DirectProvider provider = DirectProviderCache.GetInstance()[key];
context.Response.ContentType = DirectHandlerContentType;
context.Response.Write(DirectProcessor.Execute(provider, context.Request));
}
By moving these two lines:
Quote:
Ext.ns('Ext.app');
Ext.Direct.addProvider(Ext.app.REMOTING_API);
Inside of Ext.onReady method in default.aspx page, every thing is ready to go to use functions that are defined in pages. So every page has access to it's own functions only, that help organizing methods.
Note: When registering an action, it gets name of page's type, that returns 'default_aspx' instead of Default, so I had to call:
default_aspx.Echo(.....);
Alek.
PS: adding provider can be moved to Register function in helper class like this:
Quote:
string script = provider.ToString();
script += @"
Ext.ns('Ext.app');
Ext.Direct.addProvider(Ext.app.REMOTING_API);";
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "ApiHandler", script, true);
So in the page there is no need to register the provider, and it will be ready on load.
Alek.