Trigger an Ajax update from an external JS?

I’m researching a project that uses a system component loaded on the
page through a custom JS file.

On defined user actions, this component will call a definable JS method,
that i’d like to act in a link_remote_to fashion, to update some
elements of the page.

I’m just getting started with RAILS, and this doesn’t really conform to
the typical ajax updates i’ve been reading on, so i’m a bit confused on
how to actually implement this functionnality.

Should i defer the component to a vanilla JS script on the page, that
will take care of making a proper AJAX request to the back end ?
This seems a bit convoluted, and chances are i’m overlooking
something…

Any hints ?

cheers

Hi Julien,

On Thu, 2009-06-11 at 20:24 +0200, Julien J. wrote:

I’m researching a project that uses a system component loaded on the
page through a custom JS file.

I’m having a little trouble understanding exactly what you’re trying to
do. I’ll take a stab, but in case i totally misunderstand you, i’ll ask
a few questions that, if my response is no help, may make it easier to
ask again.

For starters, what’s a ‘system component’? And whatever it is, is this
the thing that you want go get loaded when the user takes some action?

On defined user actions,

So we’re talking about clicking a link or a button?

this component will call a definable JS method,

Definable? As in ‘not known until run-time’?

that i’d like to act in a link_remote_to fashion, to update some
elements of the page.

link_to_remote(…) makes an XHR / XMLHttpRequest to a controller method
on the server. In Rails you have several options WRT constructing the
JS response the client expects. The easiest is an RJS template that
replaces existing html element(s) on the page.

I highly recommend Cody F.'s O’Reilly Shortcut: RJS Templates. Best
$10 I’ve spent on Rails so far.

link_to_function(…) calls a JS function you specify in the call.

Write back if that doesn’t help.

Best regards,
Bill

thanks for taking the time to answer bill, my post was indeed vague i
must reckon, i’ll try to clear things up.

bill walton wrote:

For starters, what’s a ‘system component’? And whatever it is, is this
the thing that you want go get loaded when the user takes some action?

well it’s actually a 3d game engine plugin (s3dplayer from
www.stonetrip.com FYI), so it’s actually a system-wide component taht
relies on a JS to be loaded into the browser on page load.
Once loaded it should remain on the page, the user interacts with it,
and under certain circumstances, it will fire up JS functions to update
elements of the DOM.

On defined user actions,

So we’re talking about clicking a link or a button?

Well no, and that’s the tricky part, thanks for pointing this out.
The ‘triggering’ action would actually come from the user interacting
with the game engine component, as i explained.
The component can access the system to open a URL, so I use this to call
JS functions, with _self as the target, so the page doesn’t reload.

this component will call a definable JS method,

Definable? As in ‘not known until run-time’?

Well sort of (cf. my previous response), and that’s precisely where i
get confused.
When using link_to_function, rails knows about an actual element that
will act as a trigger (form field, some text etc), here the triggers are
just function calls to the JS layer of the client.

that i’d like to act in a link_remote_to fashion, to update some
elements of the page.

link_to_remote(…) makes an XHR / XMLHttpRequest to a controller method
on the server. In Rails you have several options WRT constructing the
JS response the client expects. The easiest is an RJS template that
replaces existing html element(s) on the page.

I highly recommend Cody F.'s O’Reilly Shortcut: RJS Templates. Best
$10 I’ve spent on Rails so far.

link_to_function(…) calls a JS function you specify in the call.

Well, I’d really like to be able to specify some DOM behavior as soon as
the triggering user-action takes place (like displaying a spinner
without relying on a JS callback), that’s what i meant by
“link_to_function fashion”, i’m thinking i might have overlooked the
traditionnal routes way though…

hope this makes more sense.

cheers

forgot to add that the game is passing parameters to the JS functions,
and i need to figure out a way to get these back to rails too…

Hi Julien,

On Thu, 2009-06-11 at 22:51 +0200, Julien J.F. wrote:

forgot to add that the game is passing parameters to the JS functions,
and i need to figure out a way to get these back to rails too…

Rails is a framework for building web-based, database-driven
applications. I’m not sure, from what you described, that Rails is what
you’re looking for.

Other than displaying the html page and associated JS, what role,
exactly, does Rails play in the application you’re developing? From
what you’ve written I don’t see where Rails would naturally enter the
picture. When you say ‘get these back to Rails’, what happens then?

Bill

On Jun 12, 12:11 am, “Julien J.F.” [email protected]
wrote:

Anyway, right now i’m merely trying to find out how to establish a solid
2 way communication between the game engine and a rails back-end (the
game engine can also execute internal code, by calling a JS function of
its wrapper).
But i’m failing to see if this can be handled purely in rails, or if
i’ll need to write some vanilla JS to act as a sort of middleware layer.

I really hope i can pull it off somehow, cause i really don’t wanna
spend the next 6 months writing php code… ^^

I think you are over complicating things here. The rails javascript
helpers are not at all magic - there’s no reason why you can’t write
that javascript yourself (and in many ways you’ll end up with much
nicer javascript if you don’t touch RJS). Ultimately both php and
rails are server side things: from the point of view of the client
side javascript there isn’t really a difference.

Fred

bill walton wrote:

Hi Julien,

On Thu, 2009-06-11 at 22:51 +0200, Julien J.F. wrote:

forgot to add that the game is passing parameters to the JS functions,
and i need to figure out a way to get these back to rails too…

Rails is a framework for building web-based, database-driven
applications. I’m not sure, from what you described, that Rails is what
you’re looking for.

Other than displaying the html page and associated JS, what role,
exactly, does Rails play in the application you’re developing? From
what you’ve written I don’t see where Rails would naturally enter the
picture. When you say ‘get these back to Rails’, what happens then?

Bill

Well for this particular prototype i’m working on, not much i must
say…

Ultimately though, the projects we have on the line would greatly
benefit from rails, but if we go that route, i need to be able to rely
solely on rails for all the business logic, and a lot of it deals with
elements/events from the game component.
Eventually i hope to be able to handle 3d objects much like dom elements
of the page.

Anyway, right now i’m merely trying to find out how to establish a solid
2 way communication between the game engine and a rails back-end (the
game engine can also execute internal code, by calling a JS function of
its wrapper).
But i’m failing to see if this can be handled purely in rails, or if
i’ll need to write some vanilla JS to act as a sort of middleware layer.

I really hope i can pull it off somehow, cause i really don’t wanna
spend the next 6 months writing php code… ^^