This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Keep reference to remote-xhr object

  • Loading branch information...
josh committed Feb 26, 2013
1 parent c6c144a commit 25fb20b3d6205024dc4eeac4f1fadf8d00d28863
Showing with 38 additions and 0 deletions.
  1. +10 −0 remote.coffee
  2. +28 −0 test/unit/remote.coffee
View
@@ -214,3 +214,13 @@ $(document).on 'submit', 'form[data-remote]', (event) ->
# 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
View
@@ -67,6 +67,20 @@ each frameworks, (framework) ->
setTimeout (-> start()), 50
+ asyncTest "link xhr is exposed via data remote-xhr", 2, ->
+ link = @$("<a data-remote href='/echo'>").appendTo('body')
+
+ @$(@document).on 'ajaxSend.test', 'a', ->
+ ok link.data 'remote-xhr'
+
+ @$(@document).on 'ajaxSuccess.test', 'a', ->
+ setTimeout ->
+ ok !link.data('remote-xhr')
+ start()
+ , 0
+
+ click link[0]
+
asyncTest "form is submitted via AJAX with GET method", 3, ->
form = @$("<form data-remote action='/echo?callback=formSubmitted'><input name=foo value=bar></form>").appendTo('body')
@@ -135,3 +149,17 @@ each frameworks, (framework) ->
form.submit()
setTimeout (-> start()), 50
+
+ asyncTest "form xhr is exposed via data remote-xhr", 2, ->
+ form = @$("<form data-remote action='/echo'><input name=foo value=bar></form>").appendTo('body')
+
+ @$(@document).on 'ajaxSend.test', 'form', ->
+ ok form.data 'remote-xhr'
+
+ @$(@document).on 'ajaxSuccess.test', 'form', ->
+ setTimeout ->
+ ok !form.data('remote-xhr')
+ start()
+ , 0
+
+ form.submit()

0 comments on commit 25fb20b

Please sign in to comment.