-
-
Save designermonkey/b5b104ec080cda62bab1 to your computer and use it in GitHub Desktop.
Revisions
-
akre54 revised this gist
Jun 23, 2015 . 1 changed file with 16 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,6 +17,17 @@ var stringifyGETParams = function(url, data) { return url; } var status = function(response) { if (response.status >= 200 && response.status < 300) { return response; } throw new Error(response.statusText); } var json = function(response) { return response.json() } Backbone.ajax = function(options) { if (options.type === 'GET' && typeof options.data === 'object') { options.url = stringifyGETParams(options.url, options.data); @@ -29,5 +40,9 @@ Backbone.ajax = function(options) { 'Content-Type': 'application/json' }), body: options.data })) .then(status) .then(json) .then(options.success) .catch(options.error); }; -
akre54 created this gist
Nov 24, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ var defaults = function(obj, source) { for (var prop in source) { if (obj[prop] === undefined) obj[prop] = source[prop]; } return obj; } var stringifyGETParams = function(url, data) { var query = ''; for (var key in data) { if (data[key] == null) continue; query += '&' + encodeURIComponent(key) + '=' + encodeURIComponent(data[key]); } if (query) url += (~url.indexOf('?') ? '&' : '?') + query.substring(1); return url; } Backbone.ajax = function(options) { if (options.type === 'GET' && typeof options.data === 'object') { options.url = stringifyGETParams(options.url, options.data); } return fetch(options.url, defaults(options, { method: options.type, headers: defaults(options.headers || {}, { 'Accept': 'application/json', 'Content-Type': 'application/json' }), body: options.data })).then(options.success, options.error); };