add extract option to m.request

This commit is contained in:
Leo Horie 2014-04-19 22:47:03 -04:00
parent cb6994dd93
commit bb54a3b675
8 changed files with 56 additions and 16 deletions

View file

@ -430,11 +430,12 @@ Mithril = m = new function app(window) {
var deferred = m.deferred()
var serialize = xhrOptions.serialize || JSON.stringify
var deserialize = xhrOptions.deserialize || JSON.parse
var extract = xhrOptions.extract || function(xhr, xhrOptions) {return xhr.responseText}
xhrOptions.url = parameterizeUrl(xhrOptions.url, xhrOptions.data)
xhrOptions = bindData(xhrOptions, xhrOptions.data, serialize)
xhrOptions.onload = xhrOptions.onerror = function(e) {
var unwrap = (e.type == "load" ? xhrOptions.unwrapSuccess : xhrOptions.unwrapError) || identity
var response = unwrap(deserialize(e.target.responseText))
var response = unwrap(deserialize(extract(e.target, xhrOptions)))
if (response instanceof Array && xhrOptions.type) {
for (var i = 0; i < response.length; i++) response[i] = new xhrOptions.type(response[i])
}