Reputation: 6096
Say you have a link like this in rails:
<%= link_to "Next page", url_for(controller: controller_name, action: 'step2'), remote: true, method: :post %>
This turns into a link with the attribute data-remote="true"
. Links with this attribute should make AJAX calls.
They do, if they are in the DOM when the page loads. The rails JS will run, parse the data-remote
attribute, and setup a listener to turn the click into an ajax call.
However, if this link is loaded dynamically, it will be inserted into the DOM after the rails js runs and parses everything. This means, if you load this link dynamically, it won't make an ajax call when you click it. It'll just redirect the whole window to the link.
Is there some function provided by the rails js that I can call manually which will re-parse the dom for these data-remote
attributes and ensure that they'll all make ajax calls?
Upvotes: 1
Views: 878
Reputation: 13716
You shouldn't need to do anything.
rails js doesn't "parse" the document when is loaded, rather, it binds the events to the document. So if you create new element and trigger an event, it'll bubble up and work anyway (like the deprecated .live()
method in jQuery
).
You can see this in action in line 290 of the rails js script
$(document).delegate(rails.linkClickSelector, 'click.rails', function(e) {
As long as you define your callbacks correctly (like ajax:success
, ajax:complete
, etc) everything should be dandy.
For more info take a look at:
Upvotes: 2