Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions backbone.nativeajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
// https://github.com/akre54/Backbone.NativeAjax

(function (factory) {
if (typeof define === 'function' && define.amd) { define(factory);
} else if (typeof exports === 'object') { module.exports = factory();
} else { Backbone.ajax = factory(); }
}(function() {
if (typeof define === 'function' && define.amd) {
define(factory.bind(this, this.XMLHttpRequest));
} else if (typeof exports === 'object') {
module.exports = factory(require('xmlhttprequest').XMLHttpRequest);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be necessary now. If it's only for testing, let's do that in the tests file.

} else {
Backbone.ajax = factory(XMLHttpRequest);
}
}(function(XMLHttpRequest) {
// Make an AJAX request to the server.
// Usage:
// var req = Backbone.ajax({url: 'url', type: 'PATCH', data: 'data'});
Expand All @@ -33,7 +37,7 @@
var isValid = function(xhr) {
return (xhr.status >= 200 && xhr.status < 300) ||
(xhr.status === 304) ||
(xhr.status === 0 && window.location.protocol === 'file:')
(xhr.status === 0 && typeof window !== "undefined" && window.location.protocol === 'file:')
};

var end = function(xhr, options, promise, resolve, reject) {
Expand Down Expand Up @@ -98,7 +102,11 @@
'=' + encodeURIComponent(value);
};
for (var key in options.data) {
query += stringifyKeyValuePair(key, options.data[key]);
var value = options.data[key];
var values = Array.isArray(value) ? value : [value];
for (var i=0; i < values.length; i++) {
query += stringifyKeyValuePair(key, values[i]);
}
}

if (query) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"devDependencies": {
"backbone": "1.2.1",
"chai": "^3.0.0",
"karma": "^0.12.36",
"karma": "^0.13.9",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do these updates separately please?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was motivated by karma failing to properly load the webpack module when running npm install && npm run-script test on a fresh checkout, which is a bug mentioned elsewhere that's fixed by upgrading karma.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the sinon change and the tests still pass on travis. Sorry for all the noise on this small PR.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good. Thanks for pointing it out :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fixed now.

"karma-chrome-launcher": "^0.1.12",
"karma-cli": "0.0.4",
"karma-firefox-launcher": "^0.1.6",
Expand Down
6 changes: 6 additions & 0 deletions test/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ describe('Backbone.NativeAjax', function() {
expect(open).to.have.been.calledOnce;
expect(open).to.have.been.calledWithExactly('GET', 'test?a=1&b=2', true);
});
it('should handle multiple values for a GET parameter', function() {
ajax({url: 'test', dataType: 'json', data: {a: [1, 2]}});

expect(open).to.have.been.calledOnce;
expect(open).to.have.been.calledWithExactly('GET', 'test?a=1&a=2', true);
})
});

describe('headers', function() {
Expand Down