Browse Source

Merge pull request #437 from santiph/patch/remoteStorage-Load-method

Set GET method on `load()` in RemoteStorage
pull/458/head
Artur Arseniev 9 years ago
committed by GitHub
parent
commit
88c262be24
  1. 29
      src/storage_manager/model/RemoteStorage.js
  2. 3
      src/utils/fetch.js

29
src/storage_manager/model/RemoteStorage.js

@ -74,7 +74,7 @@ module.exports = require('backbone').Model.extend({
},
load(keys, clb) {
this.request(this.get('urlLoad'), {body: {keys}}, clb);
this.request(this.get('urlLoad'), {method: 'get'}, clb);
},
/**
@ -91,6 +91,8 @@ module.exports = require('backbone').Model.extend({
const reqHead = 'X-Requested-With';
const typeHead = 'Content-Type';
const bodyObj = opts.body || {};
const bodilessMethods = ['get', 'head', 'options', 'delete'];
let fetchOptions;
let body;
for (let param in params) {
@ -117,19 +119,24 @@ module.exports = require('backbone').Model.extend({
body.append(bodyKey, bodyObj[bodyKey]);
}
}
this.onStart();
this.fetch(url, {
fetchOptions = {
method: opts.method || 'post',
credentials: 'include',
headers,
body,
}).then(res => (res.status/200|0) == 1 ?
res.text() : res.text().then((text) =>
Promise.reject(text)
))
.then((text) => this.onResponse(text, clb))
.catch(err => this.onError(err));
};
// Body should only be included on POST method
if (fetchOptions.method === 'post') {
fetchOptions.body = body;
}
this.onStart();
this.fetch(url, fetchOptions)
.then(res => (res.status/200|0) == 1
? res.text()
: res.text().then((text) => Promise.reject(text)))
.then((text) => this.onResponse(text, clb))
.catch(err => this.onError(err));
},
});

3
src/utils/fetch.js

@ -24,6 +24,7 @@ export default typeof fetch == 'function' ? fetch.bind() : (url, options) => {
req.upload.onprogress = options.onProgress;
}
req.send(options.body);
// Include body only if present
options.body ? req.send(options.body) : req.send();
});
}

Loading…
Cancel
Save