# Trigger an Ajax update from an external JS?

**URL:** <https://www.ruby-forum.com/t/trigger-an-ajax-update-from-an-external-js/170059>\
**Category:** Rails\
**Created:** [June 11, 2009, 8:24pm UTC](https://www.ruby-forum.com/t/trigger-an-ajax-update-from-an-external-js/170059 "2009-06-11T20:24:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![disturb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@disturb](https://www.ruby-forum.com/u/disturb)\
**Post date:** [June 11, 2009, 8:24pm UTC](https://www.ruby-forum.com/t/trigger-an-ajax-update-from-an-external-js/170059/1 "2009-06-11T20:24:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![disturb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@disturb](https://www.ruby-forum.com/u/disturb)\
**Post date:** [June 11, 2009, 9:09pm UTC](https://www.ruby-forum.com/t/trigger-an-ajax-update-from-an-external-js/170059/2 "2009-06-11T21:09:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![disturb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@disturb](https://www.ruby-forum.com/u/disturb)\
**Post date:** [June 11, 2009, 10:13pm UTC](https://www.ruby-forum.com/t/trigger-an-ajax-update-from-an-external-js/170059/3 "2009-06-11T22:13:40Z")

</div>

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](http://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

---

<div class="post-metadata">

**Author:** ![disturb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@disturb](https://www.ruby-forum.com/u/disturb)\
**Post date:** [June 11, 2009, 10:51pm UTC](https://www.ruby-forum.com/t/trigger-an-ajax-update-from-an-external-js/170059/4 "2009-06-11T22:51:40Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![disturb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@disturb](https://www.ruby-forum.com/u/disturb)\
**Post date:** [June 11, 2009, 11:48pm UTC](https://www.ruby-forum.com/t/trigger-an-ajax-update-from-an-external-js/170059/5 "2009-06-11T23:48:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![disturb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@disturb](https://www.ruby-forum.com/u/disturb)\
**Post date:** [June 12, 2009, 10:08am UTC](https://www.ruby-forum.com/t/trigger-an-ajax-update-from-an-external-js/170059/6 "2009-06-12T10:08:13Z")

</div>

On Jun 12, 12:11 am, “Julien J.F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![disturb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@disturb](https://www.ruby-forum.com/u/disturb)\
**Post date:** [June 12, 2009, 1:11am UTC](https://www.ruby-forum.com/t/trigger-an-ajax-update-from-an-external-js/170059/7 "2009-06-12T01:11:33Z")

</div>

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… ^^
