Rails - 触发来自控制器的javascript事件 [英] Rails - trigger javascript events from controllers

查看:126
本文介绍了Rails - 触发来自控制器的javascript事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑实施以下内容,并想知道a)如果由于某些原因我没有想过,可能是一个非常糟糕的主意,而且b)如果没有 - 是否有一个现有的宝石在那里要做到这一点。



基本上,我想能够从我的rails控制器排队javascript事件,以便在下一个渲染所有排队的事件将被触发并从队列。类似于Flash机制,而是事件。所以例如:



在我的控制器中:

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

,在我的应用程序布局中,如下所示:

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

任何想法?



编辑:这将是有用的例如当创建项目时,服务器可以重定向到所述项目的编辑表单,或者可以重定向到相关控制器的索引操作。这些页面不知道刚刚创建了一个项目。但是,如果项目创建成功但是如果没有创建项目,或者任何其他可能的原因,我可能需要了解它,或许我想做一些类似于关闭对话窗口的内容。显然不仅仅是与创造有关的,这只是我用来说明问题的例子。



编辑:赏金可以说服令人信服的人一种方式或另一种方式,这是一个好/坏主意,或提供一个更好的选择

解决方案

我看到你正在尝试但是你需要注意保持分开。



在这种情况下我将做的是创建一个哈希,然后是 #to_json 它在你的看法。让我们来说说,在你的行动的底部



控制器

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

index.html.erb

 < div>您通常的内容进入这里< / div> 
< script type =text / javascriptcharset =utf-8>
var events =<%= raw @ events.to_json%> //您必须确保它已正确清理
< / script>

jQuery

  $。ready(function(){
events.each(function(){
// ...用
});
});


I'm thinking of implementing something like the following and would like to know a) if it's possibly a really bad idea for some reason I haven't thought of, and b) if not - is there an existing gem out there to do it.

Basically, I'd like to be able to queue javascript events from my rails controllers so that on next render all queued events would be triggered and removed from the queue. Sort of like the flash mechanism but for events instead. So e.g.:

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: This would be useful e.g. when an item is created, the server may redirect to the edit form for said item, or may redirect to the index action for the relevant controller. These pages don't know that an item was just created. However perhaps I want to do something like close a dialog window if the item was created successfully but leave it open if it was not, or any number of other possible reasons I may need to know about it. Obviously not just relevant for creates, that's just the example I'm using to illustrate the problem

Edit: Bounty goes to whoever persuades me convincingly one way or the other that this is a good/bad idea or provides a better alternative

解决方案

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

What I would do in this situation is create a hash and then #to_json it in your view. Let's say 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's properly sanitised
</script>

jQuery

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

这篇关于Rails - 触发来自控制器的javascript事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆