Skip to content

Commit

Permalink
Merge pull request #9 from Demurgos/patch-1
Browse files Browse the repository at this point in the history
Notifications for xhr.upload.onprogress
  • Loading branch information
nathanboktae committed Jul 23, 2015
2 parents 71cea7f + 5eed510 commit 25efee7
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ Post some JSON:
})
```

Listen to progress:

```javascript

var someLargeData = getSomeLargeData();

Q.xhr.post('/processLargeData', someLargeData).progress(function(progress) {
if (progress.upload) {
console.log('Uploaded: '+progress.loaded+' bytes')
} else {
console.log('Downloaded: '+progress.loaded+' bytes')
}
}).then(function(resp) {
console.log('success!')
})
```

With modern web applications in mind, `application/json` is the default mime type.

### Differences from [Angular's $http][$http]
Expand All @@ -42,7 +59,7 @@ On the topic of MVC frameworks not needing jQuery, The [Angular] devs have adopt
- **No JSONP.** JSONP has all sorts of security flaws and limitations and causes lots of burden on both client side and server side code. Given that [XDomainRequest is available for IE8 and 9](http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx), and IE6 and 7 [are dead](http://gs.statcounter.com/#desktop-browser_version_partially_combined-ww-monthly-201302-201402), it should be avoided IMO. If you want XDomainRequest support (which jQuery never did), let me know or submit a pull request!
- **Interceptors are applied in order.** I guess [angular] had some backward compatibility they were tied to do so something funky by applying request handlers in reverse but response handlers in order, but I don't have backward compatibility issues so it works like you'd expect.
- **The default JSON transform is only applied if the response content is `application/json`**. [Angular] was doing something odd by sniffing all content via regex matching and then converting it to JSON if it matched. Why? Geez people set your `Content-Type` correctly already. Not to mention content sniffing leads to [security issues](http://blogs.msdn.com/b/ie/archive/2008/09/02/ie8-security-part-vi-beta-2-update.aspx).
- **Progress support**. Supply a progress listener function to recieve [ProgressEvent](https://developer.mozilla.org/en-US/docs/Web/API/ProgressEvent)s.
- **Progress support**. Supply a progress listener function to recieve [ProgressEvent](https://developer.mozilla.org/en-US/docs/Web/API/ProgressEvent)s. q-xhr listens to both upload and download progress. To help you detect the type of progress, q-xhr adds the boolean property `upload` to the `ProgressEvent` object.

### Installation

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "q-xhr",
"main": "q-xhr.js",
"version": "0.1.3",
"version": "1.0.0",
"homepage": "https://github.com/nathanboktae/q-xhr",
"authors": [
"Nathan Black <nathan@nathanblack.org>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "q-xhr",
"version": "0.1.3",
"version": "1.0.0",
"description": "XMLHttpRequest (ajax) using powerful Q promises",
"author": "Nathan Black <nathan@nathanblack.org>",
"main": "q-xhr.js",
Expand Down
8 changes: 8 additions & 0 deletions q-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,17 @@
}

xhr.onprogress = function (progress) {
progress.upload = false
deferred.notify(progress)
}

if (xhr.upload) {
xhr.upload.onprogress = function (progress) {
progress.upload = true
deferred.notify(progress)
}
}

if (config.withCredentials) {
xhr.withCredentials = true
}
Expand Down
25 changes: 22 additions & 3 deletions test/unit.tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,35 @@ describe 'q-xhr', ->
inFlight: ->
xhr.withCredentials.should.be.true

it 'should send progress notifications', (done) ->
it 'should send progress notifications during upload', (done) ->
if !window.XMLHttpRequestUpload
done()
return

Q.xhr(
url: '/foo'
).progress (prog) ->
prog.upload.should.equal true
prog.should.equal 'upload progress!'
done()

scenario
inFlight: ->
xhr.upload.onprogress('upload progress!')
return


it 'should send progress notifications during download', (done) ->
Q.xhr(
url: '/foo'
).progress (prog) ->
prog.should.equal 'progress!'
prog.upload.should.equal false
prog.should.equal 'download progress!'
done()

scenario
inFlight: ->
xhr.onprogress('progress!')
xhr.onprogress('download progress!')
return

describe 'params', ->
Expand Down

0 comments on commit 25efee7

Please sign in to comment.