turbograft 0.1.12 → 0.1.14

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: ea7745bcaf28ab5e4a9a3d8b480c869d0bf1e4c5
4
- data.tar.gz: c187593fb6046eb7cb3c2ac735ce83fba052ef6a
3
+ metadata.gz: 1aff9e76ad586eab0a400892c5f596ffeac608b0
4
+ data.tar.gz: 0e0c807cb3e2b1f6897de5977ae37850225b306c
5
5
  SHA512:
6
- metadata.gz: 78e4a00167e1d282fd7677f60dd3e5d9179c08c40241a30d342d49faf64472fbbd03ce9577dfaf8cd8c4e615adca889d8ce977a8e27b9d48c1c96510a1403a11
7
- data.tar.gz: c9311cb12e00474a41fd444f6f5446f85156620899446414e0af02aa5f78aa12f02d89b6fe23c01292024c54e57a4832f5c83a097ff2c7c4ccedcc4b445930ca
6
+ metadata.gz: 161fbc9a0d5312ce29364cabe5689aa8d25f9588b6504c1fde1f1a348c5daf9f47065a08e5533aa3e666d558469923ea9be4e9e26b677b1a80fcbaf5cc7aa238
7
+ data.tar.gz: 9b2346f149f5ead38e386d5a8fbff973c70075bf33018b94c3125518bc3b39b282d0a86cacc3c68c60c2595a80f78bf62c6ba427b4d07d98eb81597cf3e66b5d
data/README.md CHANGED
@@ -74,12 +74,15 @@ It requires your `<form>`, `<a>`, or `<button>` to be marked up with:
74
74
  * `tg-remote`: (optionally valueless for `<form>`, but requires an HTTP method for links) the HTTP method you wish to call on your endpoint
75
75
  * `href`: (if node is `<a>` or `<button>`) the URL of the endpoint you wish to hit
76
76
  * `refresh-on-success`: (optional) The refresh keys to be refreshed, using the body of the response. This is space-delimited
77
+ * `full-refresh-on-success-except`: (optional) Replaces body except for specififed refresh keys, using the body of the XHR which has succeeded
77
78
  * `refresh-on-error`: (optional) The refresh keys to be refreshed, but using body of XHR which has failed. Only works with error 422. If the XHR returns and error and you do not supply a refresh-on-error, nothing is changed
78
79
  * `full-refresh-on-error-except`: (optional) Replaces body except for specified refresh keys, using the body of the XHR which has failed. Only works with error 422
79
80
  * `remote-once`: (optional) The action will only be performed once. Removes `remote-method` and `remote-once` from element after consumption
80
81
  * `full-refresh`: Rather than using the content of the XHR response for partial page replacement, a full page refresh is performed. If `refresh-on-success` is defined, the page will be reloaded on these keys. If `refresh-on-success` is not defined, a full page refresh is performed. Defaults to true if neither refresh-on-success nor refresh-on-error are provided
81
82
  * `tg-remote-norefresh`: Prevents `Page.refresh()` from being called, allowing methods to be executed without updating client state
82
83
 
84
+ Note that as `refresh-on-*` pertains to partial refreshes and `full-refresh-on-*-except` pertains to full refreshes, they are incompatible with each other and should not be combined.
85
+
83
86
  ### Examples
84
87
 
85
88
  Call a remote method:
@@ -21,6 +21,7 @@ TurboGraft.handlers.remoteMethodHandler = (ev) ->
21
21
  httpUrl: httpUrl
22
22
  fullRefresh: target.getAttribute('full-refresh')?
23
23
  refreshOnSuccess: target.getAttribute('refresh-on-success')
24
+ refreshOnSuccessExcept: target.getAttribute('full-refresh-on-success-except')
24
25
  refreshOnError: target.getAttribute('refresh-on-error')
25
26
  refreshOnErrorExcept: target.getAttribute('full-refresh-on-error-except')
26
27
 
@@ -40,6 +41,7 @@ TurboGraft.handlers.remoteFormHandler = (ev) ->
40
41
  httpUrl: httpUrl
41
42
  fullRefresh: target.getAttribute('full-refresh')?
42
43
  refreshOnSuccess: target.getAttribute('refresh-on-success')
44
+ refreshOnSuccessExcept: target.getAttribute('full-refresh-on-success-except')
43
45
  refreshOnError: target.getAttribute('refresh-on-error')
44
46
  refreshOnErrorExcept: target.getAttribute('full-refresh-on-error-except')
45
47
 
@@ -8,6 +8,7 @@ class TurboGraft.Remote
8
8
  @formData = @createPayload(form)
9
9
 
10
10
  @refreshOnSuccess = @opts.refreshOnSuccess.split(" ") if @opts.refreshOnSuccess
11
+ @refreshOnSuccessExcept = @opts.refreshOnSuccessExcept.split(" ") if @opts.refreshOnSuccessExcept
11
12
  @refreshOnError = @opts.refreshOnError.split(" ") if @opts.refreshOnError
12
13
  @refreshOnErrorExcept = @opts.refreshOnErrorExcept.split(" ") if @opts.refreshOnErrorExcept
13
14
 
@@ -51,8 +52,8 @@ class TurboGraft.Remote
51
52
 
52
53
  createPayload: (form) ->
53
54
  if form
54
- if form.querySelectorAll("[type='file']").length > 0
55
- formData = new FormData(form)
55
+ if form.querySelectorAll("[type='file'][name]").length > 0
56
+ formData = @nativeEncodeForm(form)
56
57
  else # for much smaller payloads
57
58
  formData = @uriEncodeForm(form)
58
59
  else
@@ -70,6 +71,25 @@ class TurboGraft.Remote
70
71
 
71
72
  uriEncodeForm: (form) ->
72
73
  formData = ""
74
+ @_iterateOverFormInputs form, (input) =>
75
+ formData = @formAppend(formData, input.name, input.value)
76
+ formData
77
+
78
+ formDataAppend: (formData, input) ->
79
+ if input.type == 'file'
80
+ for file in input.files
81
+ formData.append(input.name, file)
82
+ else
83
+ formData.append(input.name, input.value)
84
+ formData
85
+
86
+ nativeEncodeForm: (form) ->
87
+ formData = new FormData
88
+ @_iterateOverFormInputs form, (input) =>
89
+ formData = @formDataAppend(formData, input)
90
+ formData
91
+
92
+ _iterateOverFormInputs: (form, callback) ->
73
93
  inputs = form.querySelectorAll("input:not([type='reset']):not([type='button']):not([type='submit']):not([type='image']), select, textarea")
74
94
  for input in inputs
75
95
  inputEnabled = !input.disabled
@@ -77,9 +97,7 @@ class TurboGraft.Remote
77
97
 
78
98
  if inputEnabled && input.name
79
99
  if (radioOrCheck && input.checked) || !radioOrCheck
80
- formData = @formAppend(formData, input.name, input.value)
81
-
82
- formData
100
+ callback(input)
83
101
 
84
102
  onSuccess: (ev) ->
85
103
  @opts.success?()
@@ -102,6 +120,10 @@ class TurboGraft.Remote
102
120
  Page.refresh
103
121
  response: xhr
104
122
  onlyKeys: @refreshOnSuccess
123
+ else if @refreshOnSuccessExcept
124
+ Page.refresh
125
+ response: xhr
126
+ exceptKeys: @refreshOnSuccessExcept
105
127
  else
106
128
  Page.refresh
107
129
  response: xhr
@@ -114,10 +136,13 @@ class TurboGraft.Remote
114
136
  initiator: @initiator
115
137
  xhr: xhr
116
138
 
117
- if @refreshOnError || @refreshOnErrorExcept
139
+ if @refreshOnError
118
140
  Page.refresh
119
141
  response: xhr
120
142
  onlyKeys: @refreshOnError
143
+ else if @refreshOnErrorExcept
144
+ Page.refresh
145
+ response: xhr
121
146
  exceptKeys: @refreshOnErrorExcept
122
147
  else
123
148
  triggerEventFor 'turbograft:remote:fail:unhandled', @initiator,
@@ -1,3 +1,3 @@
1
1
  module TurboGraft
2
- VERSION = '0.1.12'
2
+ VERSION = '0.1.14'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbograft
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristian Plettenberg-Dussault
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2015-01-23 00:00:00.000000000 Z
16
+ date: 2015-02-09 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: coffee-rails