Browse Source

Address PR comments

pull/437/head
Santiago Petrone 9 years ago
parent
commit
80a7153b2f
  1. 26
      src/storage_manager/model/RemoteStorage.js
  2. 9
      src/utils/fetch.js
  3. 2
      test/specs/storage_manager/model/Models.js

26
src/storage_manager/model/RemoteStorage.js

@ -74,15 +74,7 @@ module.exports = require('backbone').Model.extend({
}, },
load(keys, clb) { load(keys, clb) {
let queryString = ''; this.request(this.get('urlLoad'), {method: 'get'}, clb);
keys.forEach(function(key, index, keys) {
queryString += key;
if (index < keys.length - 1) {
queryString += ',';
}
}, this);
this.request(`${this.get('urlLoad')}?keys=${queryString}`, {method: 'get'}, clb);
}, },
/** /**
@ -133,18 +125,18 @@ module.exports = require('backbone').Model.extend({
headers, headers,
}; };
// Body should not be included on GET, HEAD, OPTIONS or DELETE methods // Body should only be included on POST method
if (!bodilessMethods.includes(fetchOptions.method)) { if (fetchOptions.method === 'post') {
fetchOptions.body = body; fetchOptions.body = body;
} }
this.onStart(); this.onStart();
this.fetch(url, fetchOptions).then(res => (res.status/200|0) == 1 ? this.fetch(url, fetchOptions)
res.text() : res.text().then((text) => .then(res => (res.status/200|0) == 1
Promise.reject(text) ? res.text()
)) : res.text().then((text) => Promise.reject(text)))
.then((text) => this.onResponse(text, clb)) .then((text) => this.onResponse(text, clb))
.catch(err => this.onError(err)); .catch(err => this.onError(err));
}, },
}); });

9
src/utils/fetch.js

@ -5,7 +5,6 @@ window.Promise = window.Promise || Promise;
export default typeof fetch == 'function' ? fetch.bind() : (url, options) => { export default typeof fetch == 'function' ? fetch.bind() : (url, options) => {
return new Promise((res, rej) => { return new Promise((res, rej) => {
const req = new XMLHttpRequest(); const req = new XMLHttpRequest();
const bodilessMethods = ['get', 'head', 'options', 'delete'];
req.open(options.method || 'get', url); req.open(options.method || 'get', url);
req.withCredentials = options.credentials == 'include'; req.withCredentials = options.credentials == 'include';
@ -25,11 +24,7 @@ export default typeof fetch == 'function' ? fetch.bind() : (url, options) => {
req.upload.onprogress = options.onProgress; req.upload.onprogress = options.onProgress;
} }
if (bodilessMethods.includes(options.method)) { // Include body only if present
req.send(); options.body ? req.send(options.body) : req.send();
} else {
req.send(options.body);
}
}); });
} }

2
test/specs/storage_manager/model/Models.js

@ -98,7 +98,7 @@ module.exports = {
obj.load(['item1', 'item2']); obj.load(['item1', 'item2']);
const callResult = obj.fetch; const callResult = obj.fetch;
expect(callResult.called).toEqual(true); expect(callResult.called).toEqual(true);
expect(callResult.firstCall.args[0]).toEqual(`${endpointLoad}?keys=item1,item2`); expect(callResult.firstCall.args[0]).toEqual(endpointLoad);
}); });
}); });

Loading…
Cancel
Save