Trigger an Ajax update from an external JS ?

Hi Julien,

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 Fauser'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 Fauser'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

Hi Julien,

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

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