-
Notifications
You must be signed in to change notification settings - Fork 428
Conversation
tldr; <body>
<div id="sidebar" data-turbolinks-permanent>
Sidebar, never changed!
</div>
<div id="flash" data-turbolinks-temporary>
You have new comments. <%= link_to 'See new comments', comments_path, change: 'comments' %>
</div>
<section id="comments:collection">
<article id="comment_123"></article>
</section>
<div>
<a id="comments:link">See 45 more comments</a>
</div>
<%= form_for Comment.new, remote: true, id: 'new_comment' do |form| %>
<% # Show validation errors %>
<%= form.text_area :content %>
<%= form.submit %>
<% end %>
</body>
<script>
# Will change #flash, #comments:collection, #comments:link
Turbolinks.visit(url, change: 'comments')
# Will change #flash, #comment_123
Turbolinks.visit(url, change: 'comment_123')
# Will only keep #sidebar
Turbolinks.visit(url)
# Will only keep #sidebar, #flash
Turbolinks.visit(url, keep: 'flash')
# Will keep nothing
Turbolinks.visit(url, flush: true)
# Same as visit() but takes a string
Turbolinks.replace(html, options)
</script> class CommentsController < ActionController::Base
def index
# renders, but keeps #sidebar
end
def create
@comment = Comment.new(comment_params)
if @comment.save
# This will change #flash, #comments:collection, #comments:link
# -> Turbolinks.visit('/comments', change: 'comments')
redirect_to comments_url, change: 'comments'
else
# Validation failure
# -> Turbolinks.replace('<%=j render :new %>', change: 'new_comment')
render :new, change: :new_comment
end
end
end |
|
||
private | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️ newline, indent all below private.
Lovely. For the record, this goes into master under the target that master is now targeting a 3.0 release. |
🎉 ❤️ 💚 💛 😍 |
Fantastic @kristianpd |
🚀 🌙 |
FYI me and @kristianpd will write docs for this next week. |
And I'm also going to try to add some automated JS tests. |
👍 🍰 |
As it is now, Something like this: Turbolinks.visit(url, change: 'search-results', change_url: true) |
Why wouldn’t Turobolinks.visit not always change the url, even when using change? Change just says “wherever we’re going, we’re just only going to change this specific DOM element”.
|
Yeah, I thought I saw the value of not changing the URL, but the more I think about it, I can't come up with a scenario where you wouldn't want it to change. Not that that means there isn't one. The relevant code currently looks like this: if doc = processResponse()
reflectNewUrl url unless options.change? || options.keep? || options.flush?
reflectRedirectedUrl() # will change url if server redirected
# ...
else
document.location.href = crossOriginRedirect() or url.absolute # will change url If change/keep/flush are set, issues arise in two cases. First, if |
I agree @kristianpd what was your reasoning behind It would be good to add some tests for these scenarios. I can work on that maybe tonight or this week-end. |
@Thibaut TBH It's from Turbograft but now that I think about it, the differentiator should really be whether you
|
@kristianpd that makes sense 👍 |
Made the change: 7387a96 |
This closes #448 by adding partial replace capabilities to Turbolinks.
@Thibaut @dhh @arthurnn