Rails - trigger javascript events from controllers

I am going to implement something like the following and would like to know: a) if it can be a really bad idea for some reason that I did not think about, and b) if not, is there a real stone for this.

Basically, I would like to be able to queue javascript events from my rails controllers so that at the next rendering all the queues in the queue are started and removed from the queue. Most likely as a flash mechanism, but for events. For example:

in my controller:

def create if resource.create add_event('resourceCreated', :id => resource.id) end end 

and in my application layout something like:

 %javascript - @events.each do |e| $(document).trigger(#{e.event_name}, #{e.event_data}) 

Any thoughts?

Edit: That would be useful, for example. when an element is created, the server can redirect to the edit form for the specified element or redirect to the index action for the corresponding controller. These pages do not know that the item has just been created. However, perhaps I want to do something like close the dialog box if the item was created successfully, but leave it open if it isn't, or any number of other possible reasons that I might need to know about. Obviously, this is not just important to create, it is just an example that I use to illustrate the problem.

Edit: The bounty goes to the one who convinces me convincingly, anyway, that this is a good / bad idea or provides a better alternative

+4
source share
4 answers

I see what you are trying to do, but you need to be careful to keep things separate.

What would I do in this situation, create a hash and then #to_json this in your view. Let them speak at the bottom of your action

controller

 @events = [ { :type => "item_created", :message => "Woohoo! Item was created"} ] 

index.html.erb

 <div>Your usual stuff goes here</div> <script type="text/javascript" charset="utf-8"> var events = <%= raw @events.to_json %> // You must make sure it properly sanitised </script> 

JQuery

 $.ready(function() { events.each(function () { // ... Do what you need to with this }); }); 
+4
source

I would suggest doing this using the Rails flash messaging engine, which already exists to inform the user of any server-side events that have occurred since the last request. Insert a hidden fragment of the machine analysis description at the end of each flash message, write a Ruby API that makes it easy to add such fragments, and then something in your view (or, possibly, written in JS) to extract and process them as needed .

As a bonus, you can also directly inform the user about events on the server side in the same movement, even if they do not have JS-compatible or available in your browser.

+2
source

Here is what you can do ...

Send user to page with givven hashtag

 = link_to "Get aWesome!", awesomem_page_path + '#initiate_awesome' 

which displays:

 /path/tosomething#initiate_awesome 

On this page, as soon as the document is loaded, type na in the string na to check the hashtags of the page, and execute js evven depending on that hashtag. An example here is shown for jQuery with Haml and using jQuery events:

 %h1 aWesomeness is about to happen... - content_for :jquery do :javascript $(document).ready(function(){ if (document.location.hash === "#initiate_awesome"){ $('#amazing_div').trigger('awesome_event'); } else { $('#not_so_cool_div').trigger('ultimate_letdown'); } }); 

With this setting, as soon as the page loads, it will trigger your js event and do what you want. Feeling acquired.

For bonus points, since this js code is in your template, you can display the event names in the js fragment from ruby ​​using the dispatcher, and write the chain of events in js for further parsing.

0
source

Hi, you can fire js events from your controller as follows.

 def create if resource.create add_event('resourceCreated', :id => resource.id) end render :update do |page| page << "dismiss()" end end 
0
source

All Articles