Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turbolinks, :remote forms and links in Phoenix?

Is there a way to use something like RoR turbolinks and :remote forms and links in Phoenix?

UPDATE

Basically remote forms and links and turbolinks is an simple approach to build dynamic JavaScript sites in cases when advanced frameworks like React or Backbone would be an overkill.

What is :remote forms and links, it's a special option for Ruby on Rails form and links helpers that instead of submitting forms and navigating links sends AJAX request to the server, the server in turn respond with some JavaScript and Browser evaluates it.

This technic makes it very quick and cheap do JavaScript forms and UI without resorting to advanced frameworks like React or Backbone.

Turbolinks is an quick and cheap approach to speed-up page transitions in web app without resorting to advanced frameworks like React or Backbone. Instead of re-loading the whole page it just updates its content.

like image 791
Alex Craft Avatar asked Apr 11 '16 13:04

Alex Craft


1 Answers

Regarding the remote forms, you can just write some javascript (or jquery in this case) yourself, something like this:

 $("body").on("submit", "form[data-remote='true']", function(e) {
    e.preventDefault();
    let $form = $(this);

    $.ajax({
      method: $form.attr("method"),
      url: $form.attr("action"),
      data: $form.serialize(),
      dataType: "script",
      beforeSend: function(_jqXHR, _settings) {
        // add a loader or whatever
      },
      complete: function(_jqXHR,_textStatus) {
        // remove a loader or whatever
      }
    });
  });
like image 151
NoDisplayName Avatar answered Sep 23 '22 15:09

NoDisplayName