[Ur] AJAX style in-page replacement?

Gian Perrone gdpe at itu.dk
Sun Dec 30 08:09:06 EST 2012


Hi all,

I'm wondering what the nicest way to achieve client-side content replacement is - conceptually, I have a server-side function that generates a bit of XML, and on a client-side button press, I want some the XML generated on the server-side to end up inside a <div> without a page reload.

I managed to make this work by doing the RPC call in the event handler for the button click, and pushing the resulting xbody fragment into a source. The <div> then contains a <dyn ...> tag, and everything works fine.  However, my first attempt was to place an <active> tag with the RPC call inside a <dyn> tag --- this appears to be disallowed (at runtime!).   Here's a minimal example:

fun serverSide (x : int) = return <xml>{[x]}</xml>

fun main () =
    foo <- source 0;
    return <xml>
        <body>
        <dyn signal={ x <- signal foo;
                      return <xml>
                        <active code={
                            f <- rpc(serverSide x);
                            return f}/>
                        </xml>
                    }/>
        <a onclick={fn _ => set foo 123}>Foo</a>
        <a onclick={fn _ => set source 456}>Bar</a>
        </body>
    </xml>

I'm unsure why this fails --- the error message generated on the client ("Error: May not 'rpc' in main thread of 'code' for <active>") is a little cryptic, and my intuition does not extend far enough to understand why the compiler accepts this in the first place only to have it fail immediately at runtime.  If my intuition around how 'active' is meant to work, this would seem the nicest way to achieve what I'm trying to do (if it actually worked...).

In any case, this seems a relatively common task.  Is my current approach with pushing around bits of XML using signals really the best way?  Is there some nicer mechanism I'm missing?

Thanks,
Gian



More information about the Ur mailing list