diff --git a/backbone.nativeajax.js b/backbone.nativeajax.js index 3f033b7..9a724c8 100644 --- a/backbone.nativeajax.js +++ b/backbone.nativeajax.js @@ -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); + } 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'}); @@ -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) { @@ -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) { diff --git a/package.json b/package.json index 711d42b..81bd82d 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "devDependencies": { "backbone": "1.2.1", "chai": "^3.0.0", - "karma": "^0.12.36", + "karma": "^0.13.9", "karma-chrome-launcher": "^0.1.12", "karma-cli": "0.0.4", "karma-firefox-launcher": "^0.1.6", diff --git a/test/ajax.js b/test/ajax.js index 8260f34..b9cb63f 100644 --- a/test/ajax.js +++ b/test/ajax.js @@ -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() {