Override render to replace the entire body given the option of turbolinks => true#20
Override render to replace the entire body given the option of turbolinks => true#20minimul wants to merge 1 commit into
Conversation
| SCRIPT | ||
| self.status = 200 | ||
| self.response_body = script | ||
| response.content_type = "text/javascript" |
| Turbolinks.clearCache(); | ||
| var parser = new DOMParser(); | ||
| var doc = parser.parseFromString("#{ActionController::Base.helpers.j(html)}", "text/html"); | ||
| document.documentElement.replaceChild(doc.body, document.body); |
There was a problem hiding this comment.
Ideally this should be Turbolinks.replace or some other public method and not a hand-crafted replacement
|
|
||
| def render(*args) | ||
| options = args.extract_options! | ||
| return super unless options[:turbolinks] |
There was a problem hiding this comment.
if we turn this option into more of a turbolinks if possible then users could just sprinkle it into the format.html and would not need to worry about another response type ...
format.html { render turbolinks: true }
return super unless options[:turbolinks] && request.xhr?
|
I'd prefer if tubolinks could somehow handle the form submission so we don't have to add more server logic ... like having a |
|
Having problems with this in that Pulling PR for now. In my app I have instead added this to application_controller.rb Example call from a controller: |
|
@minimul, if you are interested, I just packaged a solution to this problem into a gem turbolinks-form. The solution is a little tricker than just using My solution still needs some polishing but it is already working properly on Chrome, Firefox, IE>=8 and PhantomJS (for tests). |
|
I have a really simple CoffeeScript class that handles form submission nicely. Keep in mind that this is used with the new version of turbolinks that does not require jQuery. Redirects if successful, and renders and replaces entire page if not. It uses Turbolinks internal classes to do the rendering so all things work as expected like running scripts, and all data-turbolinks options. It also does a great job of handling the browser history. Hope this helps someone! in controllers that do the redirect you will want to tell turbolinks to advance the history rather than replace it: |
|
Hi @jondavidchristopher, that is a cool solution. One downside though is that your controller must be turbolinks-aware when there are redirects (this could be fixed). And also, for me specifically, I need a solution that works with forms displayed on dialogs too, where only the dialog content is replaced. I solved this issues in turbolinks-form. On the other hand I am not so comfortable with CoffeeScript, so I wrote my solution in plain Javascript, but it would be good to reuse some of the internal methods of Turbolinks like you did. |
Credit: turbolinks/turbolinks#85 (comment)