问题
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