Skip to content

Commit

Permalink
Merge pull request dropzone#742 from jmcarp/master
Browse files Browse the repository at this point in the history
Allow functions or strings for URL options.
  • Loading branch information
enyo committed Nov 29, 2014
2 parents 78c1714 + f53ea0a commit 2d1238b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/dropzone.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,11 @@ class Dropzone extends Emitter

@processQueue() if @options.autoProcessQueue

resolveOption = (option, args...) ->
if typeof option == 'function'
return option.apply(@, args)
option

# Wrapper for uploadFiles()
uploadFile: (file) -> @uploadFiles [ file ]

Expand All @@ -1065,7 +1070,9 @@ class Dropzone extends Emitter
# Put the xhr object in the file objects to be able to reference it later.
file.xhr = xhr for file in files

xhr.open @options.method, @options.url, true
method = resolveOption @options.method, files
url = resolveOption @options.url, files
xhr.open method, url, true

# Has to be after `.open()`. See https://github.com/enyo/dropzone/issues/179
xhr.withCredentials = !!@options.withCredentials
Expand Down
31 changes: 31 additions & 0 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,37 @@ describe "Dropzone", ->
dropzone.uploadFiles.callCount.should.equal 1
dropzone.uploadFiles.calledWith([ mockFile ]).should.be.ok

it "should use url options if strings", (done) ->

dropzone.addFile mockFile

setTimeout ->
expect(requests.length).to.equal 1
expect(requests[0].url).to.equal dropzone.options.url
expect(requests[0].method).to.equal dropzone.options.method
done()
, 10

it "should call url options if functions", (done) ->
method = "PUT"
url = "/custom/upload/url"

dropzone.options.method = sinon.stub().returns method
dropzone.options.url = sinon.stub().returns url

dropzone.addFile mockFile

setTimeout ->
dropzone.options.method.callCount.should.equal 1
dropzone.options.url.callCount.should.equal 1
sinon.assert.calledWith dropzone.options.method, [mockFile]
sinon.assert.calledWith dropzone.options.url, [mockFile]
expect(requests.length).to.equal 1
expect(requests[0].url).to.equal url
expect(requests[0].method).to.equal method
done()
, 10

it "should ignore the onreadystate callback if readyState != 4", (done) ->
dropzone.addFile mockFile

Expand Down

0 comments on commit 2d1238b

Please sign in to comment.