How to avoid to evaluate a JavaScript response?

删除回忆录丶 提交于 2019-12-25 12:08:17

问题


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.

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

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.
});

How can I make that?


回答1:


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: in the create.js.html:

App.createProdict() #pass the required params.

And this App.createProduct is a js behavior.



来源:https://stackoverflow.com/questions/23719173/how-to-avoid-to-evaluate-a-javascript-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!