PageMethods and Monorail Javascript Action Proxy

Recently I read an article about using Page Methods with ASP.NET Ajax (Atlas)

I’ve used this before on some projects, it’s very nice. You can basically use straight html with javascript, and make your ‘server-side’ calls via the proxy that Atlas creates with the page method.

Me being new to Monorail, I wanted to attempt the same. I figured I’d share my sample.

Monorail has a ‘Javascript Action Proxy’ article that I used to repeat the above.

My directory structure is:

Controllers
HomeController.cs

Views
Home
Index.vm (I’m using NVelocity)

In the controller I have the following ‘action’:

[Layout("Default"), Rescue("generalerror")]
public class HomeController : SmartDispatcherController
{

...

[AjaxAction]
public void sendAjax()
{
RenderText("Done");
}

...

}

In my view I have the following:

$AjaxHelper.GetJavascriptFunctions()
$AjaxHelper.GenerateJSProxy("myproxy")

function SendAjax()
{
var results = myproxy.sendAjax();
var devResults = document.getElementById("Results");
devResults.innerHTML = results;
}

Monorail will allow me to combine the benefits of using the C# language with a front-controller design and utilize existing javascript libraries. The helpers provide a mechanism to communicate from the controller to the view and vice versa.

Advertisements

3 thoughts on “PageMethods and Monorail Javascript Action Proxy

  1. That is very cool. Glad to see you were able to find the time to play with this stuff.

    The synchronous nature of this interaction is very interesting. If this were used in an onload would it impact page load time. If your user performed two clicks on the link would it perform the ajax request twice or ignore the second click.

  2. I need to investigate handling the xmlhttp events.

    ie. when the link is first clicked, you can disable the link.

    Then handle the ‘onComplete’ event to enable it again.

    That would prevent ‘double-clicking’

    two clicks would cause it to fire twice

    I’ll have to update my sample to include how to do this (once I figure it out – lol)

  3. Thanks to Hammett (one of my favorites) over at the Castle project forum clued me into an approach. ( you can find Hammett’s blog here http://hammett.castleproject.org/ )

    First off, it’s great the Monorail supports and uses the prototype.js libraries.

    So, in my sample – I altered (this is kept really simple for clarity here!)

    function SendAjax()
    {
    var myGlobalHandlers = {
    onCreate: function(){
    Element.hide(‘myButton’);
    },

    onComplete: function() {
    alert(Ajax.activeRequestCount);
    if(Ajax.activeRequestCount == 0){
    Element.show(‘myButton’);
    }
    }
    };

    Ajax.Responders.register(myGlobalHandlers);
    var results = myproxy.sendAjax();
    var devResults = document.getElementById(“Results”);
    devResults.innerHTML = results;
    }

    Hammett refers to this as a global hook.

    After implementing this code, the button is unavailable until the results are returned.

    Thanks Hammett!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s