如何避免评估JavaScript响应? [英] How to avoid to evaluate a JavaScript response?
问题描述
我正在使用Ruby on Rails4.在不评估JavaScript响应(无论它是什么),以便实现我的自定义行为.
I am using Ruby on Rails 4. In my previos question I asked about how to handle JavaScript events of a link_to :remote
element "a là Rails Way". However I would like to make the AJAX to do not evaluate the JavaScript response (whatever it is) so that I can implement my custom behaviors.
对于我来说,应通过单击以下链接来生成应忽略的AJAX响应:
In my case the AJAX response that should be ignored is generated by clicking the following link:
link_to('destroy', article_path(@article), :method => :delete, :remote => true, :id => 'css_id')
成功后,它将返回JS重定向,但是我想简单地捕获成功响应,而不评估后续的重定向.
On success it will return a JS redirect but I would like to simply catch the success response and do not evaluate the subsequent redirect.
$('#css_id').on('ajax:success', function(event, xhr, status) {
alert("success!");
\\ Here I would like to do not evaluate the JS response.
});
我该怎么做?
推荐答案
您可以编写JavaScript行为,并仅在.js.erb文件中调用此行为,因为可以避免在模板内编写更多js代码.应该是这样的.
You can write a javascript behavior and just call this behavior in your .js.erb file since you can avoid writing more js codes inside the template. It should be something like this.
EX:在create.js.html中:
EX: in the create.js.html:
App.createProdict() #pass the required params.
这个App.createProduct是一个js行为.
And this App.createProduct is a js behavior.
这篇关于如何避免评估JavaScript响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!