PDA

View Full Version : Calling Direct Method from client side .js



AnFil
Nov 01, 2020, 6:48 PM
Hello again,

sorry for persisting on the subject.
In our current Ext.Net 3 architecture we rely heavily on calling Direct Methods from client JS functions.
I appreciate the Direct Event example provided above, but what interest us is what is the equivalent of adding a [Direct Method] attribute to a function in order to make it accessible in the App.Direct client side namespace?

We basically use Web Forms in its most basic format, executing ASP.NET code only in the Page_Load method. Everything else happens in [Direct Method] functions. So, essentially, we do not use [Direct Events] which require binding to specific DOM Components.
In a similar approach, can we have code execute server side without having to use a Direct Event but rather call a Direct Method as per below?

Client Side:

function do_register() {

App.direct.Register(
{
success: function (result) {
if (result) {
close_register();
}
},
eventMask: { showMask: true, msg: App.WAIT },
failure: function (errorMsg) { Ext.Msg.alert(App.PROBLEM, errorMsg); }
});
}



Server side:


[DirectMethod]
public bool Register()
{
//some code
return false;
}


Thank you very much for your time.
Kind regards.

fabricio.murta
Nov 02, 2020, 6:57 PM
Hello @AnFil!

I have moved your post to a dedicated thread in the 7.x classic forums (https://forums.ext.net/forumdisplay.php?51), hope it's not a problem, as your inquire would be just hidden somewhere or just that it was going a little away from the initial thread's topic.

Anyway, you had it almost done with title and all, so I just moved it. :-)

To use direct methods in Ext.NET 7, you should make similar annotations to the class and methods; in a simplistic approach with a direct method that just sleeps for 8 seconds on a default Index RazorPage, you'd have its controller/model code like this:



[DirectModel]
public class IndexModel : PageModel
{
public void OnGet()
{

}

[Direct]
public IActionResult OnPostSomeDirectMethod()
{
System.Threading.Thread.Sleep(8000);
return this.Direct();
}
}


Then in the index page you could call it anytime from client side by App.direct.SomeDirectMethod().

From this point you can go ahead and experiment with passing parameters, returning code, changing components' state.

Notice outside of WebForms world, we won't have a this.MyComponentsId reference from code behind anymore. To get a handle from a component given its ID, it is a similar approach to Ext.NET MVC: this.GetCmp<ComponentType>("MyComponentsId")

We will add an example using direct methods to Examples explorer the next time it is refereshed, it will be equivalent to the Version 5's Events > Direct Events > Duration Messages (https://examples5.ext.net/#/Events/DirectEvents/Duration_Messages/) (it does have one case within it using a direct method and not only direct events).

Hope this helps!

AnFil
Nov 02, 2020, 8:33 PM
Hello Fabricio,

This helps clear things up a lot.
I will experiment and see how far i get with porting out ExtNet3 code :)

Thank you very much for your help.

geoffrey.mcgill
Nov 02, 2020, 9:00 PM
We are working on an enhancement that will enable returning a value from the Direct Method.


[Direct]
public IActionResult OnPostRegister
{
return this.Direct(false);
}


On the client, you would call using the same code as you provided above; `App.direct.Register(...)`.

We're hoping this will be included in the next Ext.NET Classic release (7.2.0).