turbolinks-form 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 68b295d51c1948564055c96aa9633189b406fe99
4
- data.tar.gz: a0f9a39e4266b8677eca99642688490115dd9ae3
3
+ metadata.gz: 84fcc12adbcab8aa0ea501f1414779cc1280316b
4
+ data.tar.gz: 9f15476b1564cf520839ef954466dd5f9759ef16
5
5
  SHA512:
6
- metadata.gz: 48b377206054822b6e8e9917715a7debd49d0b85846d67fb50c835adc049a47dd5cb380ae8715d5bfdf8b988d10c433199bd02d74142c5528f9c4659297e8635
7
- data.tar.gz: e730d8f0d459c64082dd184a4941d37b5c9817d703fcf827b5fc5b891b7dcf16bcb308a515b4bbefa00e1df952fcc1fc667056d625bc3831395b93cf8047136d
6
+ metadata.gz: c901c6dd2871e074c77b9e877b6bea65105da49717c9713568505942ce0ef20ed7605b8e7dd8a524c8910c17f7fb87b57706d071fc894bb8a0adea11b4b33c0d
7
+ data.tar.gz: 61b32ad2e7578b9c06572807b4f6cc52fff3fb06c1bfb8d771032520396f71b3bdd900979701462782453b91095a1ccf3639db1560474bb9060a2d50c68d710a
@@ -19,7 +19,7 @@
19
19
 
20
20
  var TurbolinksForm = function(){};
21
21
 
22
- TurbolinksForm.handleResponse = function(response) {
22
+ TurbolinksForm.handleResponse = function(response, renderingError) {
23
23
  // parses response
24
24
  var newDom = new DOMParser().parseFromString(response.responseText, "text/html");
25
25
 
@@ -41,6 +41,14 @@ TurbolinksForm.handleResponse = function(response) {
41
41
 
42
42
  // console.log('before-render')
43
43
 
44
+ // replaces whole head if rendering an error page. Useful when handling 500 error, since applications
45
+ // usually have a whole different set of styles for this page. This is consistent with Turbolinks.
46
+ if (renderingError) {
47
+ // head replacement doesn't work the same way as body replacement. We must use the replaceChild()
48
+ // function for it to work effectively
49
+ document.documentElement.replaceChild(newDom.head, document.head);
50
+ }
51
+
44
52
  // Removes/saves all script tags contents.
45
53
  // Most browsers don't run the new <script> tags when we replace the page body,
46
54
  // but some do (like PhantomJS). So we clear all script tags to ensure nothing
@@ -55,7 +63,7 @@ TurbolinksForm.handleResponse = function(response) {
55
63
 
56
64
  // if there is no target, replaces whole body
57
65
  var target;
58
- if (!response.getResponseHeader('turbolinks-form-render-target')) {
66
+ if (!response.getResponseHeader('turbolinks-form-render-target') || renderingError) {
59
67
  document.body = newDom.body;
60
68
  target = document.body;
61
69
  } else {
@@ -91,22 +99,33 @@ TurbolinksForm.handleResponse = function(response) {
91
99
  // 1) HTTP status code is 422 unprocessable_entity
92
100
  // 2) Response has the 'turbolinks-form-render' header
93
101
  //
94
- // PS: it is also activated on errors with code 500, so that we can know the
102
+ // PS: it is also activated on errors with code 500 or 404, so that we can know the
95
103
  // error is happening and not that the site is unresponsive
96
104
  $(document).on("ajax:error", function(e, response) {
97
105
  // dispatches turbolinks event
98
106
  Turbolinks.dispatch('turbolinks:request-end', {data: {xhr: response}});
99
107
 
100
- var isError500 = (response.status == 500)
108
+ // handles form error (replaces body/target only, does not touch head)
101
109
  var isFormErrorResponse = (response.status == 422 && response.getResponseHeader('turbolinks-form-render'));
102
- if (isError500 || isFormErrorResponse) {
110
+ if (isFormErrorResponse) {
103
111
  TurbolinksForm.handleResponse(response);
112
+ return;
113
+ }
114
+
115
+ // replaces whole body and whole head when a true error occurs
116
+ var isError500 = (response.status == 500)
117
+ var isError404 = (response.status == 404)
118
+ if (isError500 || isError404) {
119
+ TurbolinksForm.handleResponse(response, true);
120
+ return;
104
121
  }
105
122
  });
106
123
 
107
124
  // This code is only activated if:
108
125
  // 1) HTTP status code is 200
109
126
  // 2) Response has 'turbolinks-form-render' header and 'turbolinks-form-render-when-success' header
127
+ //
128
+ // This handling is useful when we dont want a redirect after a successful submit
110
129
  $(document).on("ajax:success", function(e, data, status, response) {
111
130
  // dispatches turbolinks event
112
131
  Turbolinks.dispatch('turbolinks:request-end', {data: {xhr: response}});
@@ -1,5 +1,5 @@
1
1
  module Turbolinks
2
2
  module Form
3
- VERSION = '0.0.7'
3
+ VERSION = '0.0.8'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbolinks-form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique Gubert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-24 00:00:00.000000000 Z
11
+ date: 2019-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails