This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathremote.coffee
226 lines (208 loc) · 6.47 KB
/
remote.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Remote
#
#= require ./beforesend
#
# Implements `data-remote` for forms and links.
#
#
# ### Markup
#
# `<a>`
#
# ``` definition-table
# Attribute - Description
#
# `data-remote`
# Enables remote behavior on the element if set to anything.
#
# `data-method`
# Changes the method of the AJAX request. Defaults to `"get"`. Maps to
# jQuery's [type](http://api.jquery.com/jQuery.ajax/).
#
# `href`
# URL for AJAX request. Defaults to the current url. Maps to jQuery's
# [`url`](http://api.jquery.com/jQuery.ajax/).
#
# `data-type`
# Specify the data type of the response. Can be `"xml"`, `"json"`,
# `"script"`, or `"html"`. Defaults to jQuery's *"intelligent guess"*
# from the response content type. Maps to jQuery's
# [`dataType`](http://api.jquery.com/jQuery.ajax/).
# ```
#
# ``` html
# <a href="/toggle" data-method="post" data-remote>Toggle</a>
# ```
#
# `<form>`
#
# ``` definition-table
# Attribute - Description
#
# `data-remote`
# Enables remote behavior on the element if set to anything.
#
# `method`
# Changes the method of the AJAX request. Defaults to `"get"`. Maps to
# jQuery's [type](http://api.jquery.com/jQuery.ajax/).
#
# `action`
# URL for AJAX request. Defaults to the current url. Maps to jQuery's
# [`url`](http://api.jquery.com/jQuery.ajax/).
#
# `data-type`
# Specify the data type of the response. Can be `"xml"`, `"json"`,
# `"script"`, or `"html"`. Defaults to jQuery's *"intelligent guess"*
# from the response content type. Maps to jQuery's
# [`dataType`](http://api.jquery.com/jQuery.ajax/).
# ```
#
# ``` html
# <form action="/comment" method="post" data-remote></form>
# ```
#
#
# ### Events
#
# Use delegated jQuery's global AJAX events to handle successful
# and error states. See jQuery's [AJAX event
# documentation](http://docs.jquery.com/Ajax_Events) for a complete
# reference.
#
# `ajaxBeforeSend`
#
# This event, which is triggered before an Ajax request is started, allows you to modify the XMLHttpRequest object (setting additional headers, if need be.)
#
# ``` definition-table
# Property - Value
#
# Synchronicity - Sync
# Bubbles - Yes
# Cancelable - Yes
# Default action - Sends request
# Target - `a` or `form` element with `[data-remote]`
# Extra arguments
# [`jqXHR`](http://api.jquery.com/jQuery.ajax/#jqXHR) - XMLHttpRequest like object
# [`settings`](http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings) - Object passed as `$.ajax` settings
# ```
#
# `ajaxSuccess`
#
# This event is only called if the request was successful (no errors from the server, no errors with the data).
#
# ``` definition-table
# Property - Value
#
# Synchronicity - Sync
# Bubbles - Yes
# Cancelable - No
# Target - `a` or `form` element with `[data-remote]`
# Extra arguments
# [`jqXHR`](http://api.jquery.com/jQuery.ajax/#jqXHR) - XMLHttpRequest like object
# [`settings`](http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings) - Object passed as `$.ajax` settings
# `data` - The data returned from the server
# ```
#
# `ajaxError`
#
# This event is only called if an error occurred with the request (you can never have both an error and a success callback with a request).
#
# ``` definition-table
# Property - Value
#
# Synchronicity - Sync
# Bubbles - Yes
# Cancelable - No
# Target - `a` or `form` element with `[data-remote]`
# Extra arguments
# [`jqXHR`](http://api.jquery.com/jQuery.ajax/#jqXHR) - XMLHttpRequest like object
# `textStatus` - `"timeout"`, `"error"`, `"abort"`, or `"parsererror"`
# `errorThrown` - Exception object
# ```
#
# `ajaxComplete`
#
# This event is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests.
#
# ``` definition-table
# Property - Value
#
# Synchronicity - Sync
# Bubbles - Yes
# Cancelable - No
# Target - `a` or `form` element with `[data-remote]`
# Extra arguments
# [`jqXHR`](http://api.jquery.com/jQuery.ajax/#jqXHR) - XMLHttpRequest like object
# `textStatus` - `"timeout"`, `"error"`, `"abort"`, or `"parsererror"`
# ```
#
# $(document).on 'ajaxBeforeSend', '.new-comment', ->
# $(this).addClass 'loading'
#
# $(document).on 'ajaxComplete', '.new-comment', ->
# $(this).removeClass 'loading'
#
# $(document).on 'ajaxSuccess', '.new-comment', (event, xhr, settings, data) ->
# $('.comments').append data.commentHTML
#
# $(document).on 'ajaxError', '.new-comment', ->
# alert "Something went wrong!"
#
# Intercept all clicked links with data-remote and turn
# it into a XHR request instead.
$(document).on 'click', 'a[data-remote]', (event) ->
element = $(this)
settings = {}
# Setting `context` to the element will cause all global
# AJAX events to bubble up from it.
settings.context = this
# Allow AJAX method to be changed using the `data-method` attribute
# <a href="/comment" data-remote data-method="post">
if type = element.attr 'data-method'
settings.type = type
# Use anchor href as the AJAX url
if url = this.href
settings.url = url
# Allow dataType to be changed using the `data-type` attribute
# <a href="/comment" data-remote data-type="json">
if dataType = element.attr 'data-type'
settings.dataType = dataType
# Do it
$.ajax settings
# Prevent default action so we don't follow the link
event.preventDefault()
false
# Intercept all form submissions with data-remote and turn
# it into a XHR request instead.
$(document).on 'submit', 'form[data-remote]', (event) ->
form = $(this)
settings = {}
# Setting `context` to the form will cause all global
# AJAX events to bubble up from it.
settings.context = this
# Use form method as the AJAX method
if type = form.attr 'method'
settings.type = type
# Use form action as the AJAX url
if url = this.action
settings.url = url
# Seralize form inputs
if data = form.serializeArray()
settings.data = data
# Allow dataType to be changed using the `data-type` attribute
# <a href="/comment" data-remote data-type="json">
if dataType = form.attr 'data-type'
settings.dataType = dataType
# Do it
$.ajax settings
# Prevent default action and don't actually submit the form
event.preventDefault()
false
# Hold a reference to sent XHR object.
$(document).on 'ajaxSend', '[data-remote]', (event, xhr) ->
$(this).data 'remote-xhr', xhr
return
# Clear reference to completed XHR object.
$(document).on 'ajaxComplete', '[data-remote]', (event, xhr) ->
$(this).removeData? 'remote-xhr'
return