turbograft 0.1.3 → 0.1.4

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: fdfceb6ad8b2e34e8d9ce6d0a69349ba600b1345
4
- data.tar.gz: 42e22d98b1307ebbd2e48ae1ce872e3bdc06a388
3
+ metadata.gz: d0cf7f7c936415c6a11516f9a43f533f5b67b178
4
+ data.tar.gz: ad506629d8ad4c6c27b778b3b2bfabbb40ca70ad
5
5
  SHA512:
6
- metadata.gz: 2871b128d1379d4944ede546d25a2e563eafcfe4faab8090742b75679a88dd27710a224b092976845d3e0716e0d5a9e93a43379eb0f316ee05176dbdb3c1002c
7
- data.tar.gz: 179aaf1e7a524cb56af76b06e002f7ae8556541589b1b9ebc32a68058931f1f46e4b2e595f112cf99e7b9a2207f88d44e9729ba13daef7ce6aaee290ea8a4d91
6
+ metadata.gz: 4d98dd4f884babcf6538344e42ae3e728877711d37bc38cca97ca837eb7b2e606e32e455d4f33fca7b57fa21a3f53e7c57e5aabd51f0cbaaa6a0a82ff37e68af
7
+ data.tar.gz: 54c66bd97efdbb41bdafb7a0edf710bb5f9b8a85a358fe3b9480052ce34159c5556f4494a9109b9c2922bcd9f3be01ca8d07a9166c75139def907e29eb59f59d
@@ -36,6 +36,7 @@ TurboGraft.handlers.remoteFormHandler = (ev) ->
36
36
  throw new Error("Turbograft developer error: You did not provide a URL ('action' attribute) for tg-remote") unless httpUrl
37
37
 
38
38
  options =
39
+ httpRequestType: target.getAttribute('method')
39
40
  httpUrl: httpUrl
40
41
  fullRefresh: target.getAttribute('full-refresh')?
41
42
  refreshOnSuccess: target.getAttribute('refresh-on-success')
@@ -3,16 +3,20 @@ class TurboGraft.Remote
3
3
 
4
4
  @initiator = form || target
5
5
 
6
- @formData = @createPayload(form)
6
+ @actualRequestType = if @opts.httpRequestType?.toLowerCase() == 'get' then 'GET' else 'POST'
7
7
 
8
- actualRequestType = if @opts.httpRequestType?.toLowerCase() == 'get' then 'GET' else 'POST'
8
+ @formData = @createPayload(form)
9
9
 
10
10
  @refreshOnSuccess = @opts.refreshOnSuccess.split(" ") if @opts.refreshOnSuccess
11
11
  @refreshOnError = @opts.refreshOnError.split(" ") if @opts.refreshOnError
12
12
  @refreshOnErrorExcept = @opts.refreshOnErrorExcept.split(" ") if @opts.refreshOnErrorExcept
13
13
 
14
14
  xhr = new XMLHttpRequest
15
- xhr.open(actualRequestType, @opts.httpUrl, true)
15
+ if @actualRequestType == 'GET'
16
+ url = if @formData then @opts.httpUrl + "?#{@formData}" else @opts.httpUrl
17
+ xhr.open(@actualRequestType, url, true)
18
+ else
19
+ xhr.open(@actualRequestType, @opts.httpUrl, true)
16
20
  xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
17
21
  xhr.setRequestHeader('Accept', 'text/html, application/xhtml+xml, application/xml')
18
22
  xhr.setRequestHeader("Content-Type", @contentType) if @contentType
@@ -51,15 +55,20 @@ class TurboGraft.Remote
51
55
  else # for much smaller payloads
52
56
  formData = @uriEncodeForm(form)
53
57
  else
54
- formData = new FormData()
58
+ formData = ''
55
59
 
56
60
  if formData instanceof FormData
57
61
  formData.append("_method", @opts.httpRequestType) if @opts.httpRequestType
58
62
  else
59
63
  @contentType = "application/x-www-form-urlencoded; charset=UTF-8"
64
+ formData = @formAppend(formData, "_method", @opts.httpRequestType) if formData.indexOf("_method") == -1 && @opts.httpRequestType && @actualRequestType != 'GET'
60
65
 
61
66
  formData
62
67
 
68
+ formAppend: (uriEncoded, key, value) ->
69
+ uriEncoded += "&" if uriEncoded.length
70
+ uriEncoded += "#{encodeURIComponent(key)}=#{encodeURIComponent(value)}"
71
+
63
72
  uriEncodeForm: (form) ->
64
73
  formData = ""
65
74
  inputs = form.querySelectorAll("input:not([type='reset']):not([type='button']):not([type='submit']):not([type='image']), select, textarea")
@@ -69,9 +78,8 @@ class TurboGraft.Remote
69
78
 
70
79
  if inputEnabled && input.name
71
80
  if (radioOrCheck && input.checked) || !radioOrCheck
72
- formData += "#{encodeURIComponent(input.name)}=#{encodeURIComponent(input.value)}&"
81
+ formData = @formAppend(formData, input.name, input.value)
73
82
 
74
- formData = formData.slice(0,-1) if formData.charAt(formData.length - 1) == "&"
75
83
  formData
76
84
 
77
85
  onSuccess: (ev) ->
@@ -86,17 +94,18 @@ class TurboGraft.Remote
86
94
  Page.visit(redirect, reload: true)
87
95
  return
88
96
 
89
- if @opts.fullRefresh && @refreshOnSuccess
90
- Page.refresh(onlyKeys: @refreshOnSuccess)
91
- else if @opts.fullRefresh
92
- Page.refresh()
93
- else if @refreshOnSuccess
94
- Page.refresh
95
- response: xhr
96
- onlyKeys: @refreshOnSuccess
97
- else
98
- Page.refresh
99
- response: xhr
97
+ unless @initiator.hasAttribute('tg-remote-norefresh')
98
+ if @opts.fullRefresh && @refreshOnSuccess
99
+ Page.refresh(onlyKeys: @refreshOnSuccess)
100
+ else if @opts.fullRefresh
101
+ Page.refresh()
102
+ else if @refreshOnSuccess
103
+ Page.refresh
104
+ response: xhr
105
+ onlyKeys: @refreshOnSuccess
106
+ else
107
+ Page.refresh
108
+ response: xhr
100
109
 
101
110
  onError: (ev) ->
102
111
  @opts.fail?()
@@ -1,3 +1,3 @@
1
1
  module TurboGraft
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
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.3
4
+ version: 0.1.4
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: 2014-12-10 00:00:00.000000000 Z
16
+ date: 2014-12-15 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: coffee-rails
@@ -183,6 +183,34 @@ dependencies:
183
183
  - - ">="
184
184
  - !ruby/object:Gem::Version
185
185
  version: '0'
186
+ - !ruby/object:Gem::Dependency
187
+ name: pry
188
+ requirement: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ type: :development
194
+ prerelease: false
195
+ version_requirements: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - ">="
198
+ - !ruby/object:Gem::Version
199
+ version: '0'
200
+ - !ruby/object:Gem::Dependency
201
+ name: pry-byebug
202
+ requirement: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ type: :development
208
+ prerelease: false
209
+ version_requirements: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - ">="
212
+ - !ruby/object:Gem::Version
213
+ version: '0'
186
214
  description: It's like turbolinks, but with partial page replacement and tests
187
215
  email:
188
216
  - tylermercier@gmail.com