Merge pull request #1499 from isiahmeadows/legacy-docs
Fix added `header` parameter + update docs, fix a few nits
This commit is contained in:
commit
c156b484a6
7 changed files with 154 additions and 99 deletions
1
.gitattributes
vendored
1
.gitattributes
vendored
|
|
@ -1,2 +1,3 @@
|
||||||
* text eol=crlf
|
* text eol=crlf
|
||||||
*.min.js binary
|
*.min.js binary
|
||||||
|
*.map binary
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
- [Using variable data formats](#using-variable-data-formats)
|
- [Using variable data formats](#using-variable-data-formats)
|
||||||
- [Extracting Metadata from the Response](#extracting-metadata-from-the-response)
|
- [Extracting Metadata from the Response](#extracting-metadata-from-the-response)
|
||||||
- [Custom request rejections](#custom-request-rejections)
|
- [Custom request rejections](#custom-request-rejections)
|
||||||
|
- [Setting headers](#setting-headers)
|
||||||
- [Configuring the underlying XMLHttpRequest](#configuring-the-underlying-xmlhttprequest)
|
- [Configuring the underlying XMLHttpRequest](#configuring-the-underlying-xmlhttprequest)
|
||||||
- [Aborting a request](#aborting-a-request)
|
- [Aborting a request](#aborting-a-request)
|
||||||
- [Using JSON-P](#using-json-p)
|
- [Using JSON-P](#using-json-p)
|
||||||
|
|
@ -337,15 +338,34 @@ You can read more about the [promise exception monitor here](mithril.deferred.md
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
### Setting headers
|
||||||
|
|
||||||
|
The `headers` option can be used to add or modify existing headers. The example The example below shows how to configure a `POST` request where the server expects requests to have a `Content-Type: application/json` header.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
m.request({
|
||||||
|
method: "POST",
|
||||||
|
url: "/foo",
|
||||||
|
headers: {"Content-Type": "application/json"}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
### Configuring the underlying XMLHttpRequest
|
### Configuring the underlying XMLHttpRequest
|
||||||
|
|
||||||
The `config` option can be used to arbitrarily configure the native XMLHttpRequest instance and to access properties that would not be accessible otherwise.
|
The `config` option can be used to arbitrarily configure the native XMLHttpRequest instance and to access properties that would not be accessible otherwise. For example, this is how to listen for progress notifications.
|
||||||
|
|
||||||
The example below shows how to configure a request where the server expects requests to have a `Content-Type: application/json` header
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var xhrConfig = function(xhr) {
|
var xhrConfig = function(xhr) {
|
||||||
xhr.setRequestHeader("Content-Type", "application/json");
|
xhr.onprogress = function(ev) {
|
||||||
|
ev = ev || event;
|
||||||
|
if (ev.lengthComputable) {
|
||||||
|
console.log(ev.loaded + " bytes sent out of " + ev.total + " total.")
|
||||||
|
} else {
|
||||||
|
console.log(ev.loaded + " bytes sent.")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m.request({method: "POST", url: "/foo", config: xhrConfig});
|
m.request({method: "POST", url: "/foo", config: xhrConfig});
|
||||||
|
|
@ -622,9 +642,13 @@ where:
|
||||||
|
|
||||||
And the data is `[{name: "John"}, {name: "Mary"}]`, then the response will contain an array of two User instances.
|
And the data is `[{name: "John"}, {name: "Mary"}]`, then the response will contain an array of two User instances.
|
||||||
|
|
||||||
|
- **Object<String> headers** (optional)
|
||||||
|
|
||||||
|
Additional headers to set on the request, if any. Each header is specified as a key-value pair.
|
||||||
|
|
||||||
- **XMLHttpRequest? config(XMLHttpRequest xhr, XHROptions options)** (optional)
|
- **XMLHttpRequest? config(XMLHttpRequest xhr, XHROptions options)** (optional)
|
||||||
|
|
||||||
An initialization function that runs after `open` and before `send`. Useful for adding request headers and when using XHR2 features, such as the XMLHttpRequest's `upload` property.
|
An initialization function that runs after `open` and before `send`. Useful for using XHR2 features, such as the XMLHttpRequest's `upload` property or `progress` event.
|
||||||
|
|
||||||
- **XMLHttpRequest xhr**
|
- **XMLHttpRequest xhr**
|
||||||
|
|
||||||
|
|
@ -671,5 +695,3 @@ where:
|
||||||
- **Object<any> data** (optional)
|
- **Object<any> data** (optional)
|
||||||
|
|
||||||
Data to be sent. It's automatically placed in the appropriate section of the request with the appropriate serialization based on `method`
|
Data to be sent. It's automatically placed in the appropriate section of the request with the appropriate serialization based on `method`
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
30
mithril.js
30
mithril.js
|
|
@ -7,11 +7,11 @@
|
||||||
/* Set dependencies when no window for isomorphic compatibility */
|
/* Set dependencies when no window for isomorphic compatibility */
|
||||||
if(typeof window === "undefined") {
|
if(typeof window === "undefined") {
|
||||||
m.deps({
|
m.deps({
|
||||||
document: typeof document !== "undefined"? document: {},
|
document: typeof document !== "undefined" ? document : {},
|
||||||
location: typeof location !== "undefined"? location: {},
|
location: typeof location !== "undefined" ? location : {},
|
||||||
clearTimeout: clearTimeout,
|
clearTimeout: clearTimeout,
|
||||||
setTimeout: setTimeout
|
setTimeout: setTimeout
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
if (typeof module === "object" && module != null && module.exports) {
|
if (typeof module === "object" && module != null && module.exports) {
|
||||||
module.exports = m
|
module.exports = m
|
||||||
|
|
@ -524,7 +524,8 @@
|
||||||
parentTag
|
parentTag
|
||||||
) {
|
) {
|
||||||
var nodes = cached.nodes
|
var nodes = cached.nodes
|
||||||
if (!editable || editable !== $document.activeElement || data !== cached) {
|
if (!editable || editable !== $document.activeElement ||
|
||||||
|
data !== cached) {
|
||||||
if (data.$trusted) {
|
if (data.$trusted) {
|
||||||
clear(nodes, cached)
|
clear(nodes, cached)
|
||||||
nodes = injectHTML(parentElement, index, data)
|
nodes = injectHTML(parentElement, index, data)
|
||||||
|
|
@ -1095,10 +1096,8 @@
|
||||||
// #1252 likewise when `contenteditable` is set on an element.
|
// #1252 likewise when `contenteditable` is set on an element.
|
||||||
try {
|
try {
|
||||||
if (
|
if (
|
||||||
tag !== "input" && !node.isContentEditable
|
tag !== "input" && !node.isContentEditable ||
|
||||||
/* eslint-disable eqeqeq */
|
node[attrName] != dataAttr // eslint-disable-line eqeqeq
|
||||||
|| node[attrName] != dataAttr
|
|
||||||
/* eslint-enable eqeqeq */
|
|
||||||
) {
|
) {
|
||||||
node[attrName] = dataAttr
|
node[attrName] = dataAttr
|
||||||
}
|
}
|
||||||
|
|
@ -1436,6 +1435,8 @@
|
||||||
return parameterize(component, args)
|
return parameterize(component, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var currentRoute, previousRoute
|
||||||
|
|
||||||
function checkPrevented(component, root, index, isPrevented) {
|
function checkPrevented(component, root, index, isPrevented) {
|
||||||
if (!isPrevented) {
|
if (!isPrevented) {
|
||||||
m.redraw.strategy("all")
|
m.redraw.strategy("all")
|
||||||
|
|
@ -1468,6 +1469,7 @@
|
||||||
if (component == null) {
|
if (component == null) {
|
||||||
removeRootElement(root, index)
|
removeRootElement(root, index)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (previousRoute) {
|
if (previousRoute) {
|
||||||
currentRoute = previousRoute
|
currentRoute = previousRoute
|
||||||
}
|
}
|
||||||
|
|
@ -1604,7 +1606,7 @@
|
||||||
var modes = {pathname: "", hash: "#", search: "?"}
|
var modes = {pathname: "", hash: "#", search: "?"}
|
||||||
var redirect = noop
|
var redirect = noop
|
||||||
var isDefaultRoute = false
|
var isDefaultRoute = false
|
||||||
var routeParams, currentRoute, previousRoute
|
var routeParams
|
||||||
|
|
||||||
m.route = function (root, arg1, arg2, vdom) { // eslint-disable-line
|
m.route = function (root, arg1, arg2, vdom) { // eslint-disable-line
|
||||||
// m.route()
|
// m.route()
|
||||||
|
|
@ -2166,11 +2168,6 @@
|
||||||
xhr.setRequestHeader("Accept", "application/json, text/*")
|
xhr.setRequestHeader("Accept", "application/json, text/*")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isFunction(options.config)) {
|
|
||||||
var maybeXhr = options.config(xhr, options)
|
|
||||||
if (maybeXhr != null) xhr = maybeXhr
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isObject(options.headers)) {
|
if (isObject(options.headers)) {
|
||||||
for (var header in options.headers) {
|
for (var header in options.headers) {
|
||||||
if (hasOwn.call(options.headers, header)) {
|
if (hasOwn.call(options.headers, header)) {
|
||||||
|
|
@ -2179,6 +2176,11 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isFunction(options.config)) {
|
||||||
|
var maybeXhr = options.config(xhr, options)
|
||||||
|
if (maybeXhr != null) xhr = maybeXhr
|
||||||
|
}
|
||||||
|
|
||||||
var data = options.method === "GET" || !options.data ? "" : options.data
|
var data = options.method === "GET" || !options.data ? "" : options.data
|
||||||
|
|
||||||
if (data && !isString(data) && data.constructor !== global.FormData) {
|
if (data && !isString(data) && data.constructor !== global.FormData) {
|
||||||
|
|
|
||||||
2
mithril.min.js
vendored
2
mithril.min.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -146,7 +146,6 @@ describe("m.request()", function () {
|
||||||
expect(xhr.$headers).to.not.have.property("Content-Type")
|
expect(xhr.$headers).to.not.have.property("Content-Type")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
it("sets xhr request headers as per the headers config", function () {
|
it("sets xhr request headers as per the headers config", function () {
|
||||||
var error = m.prop()
|
var error = m.prop()
|
||||||
|
|
||||||
|
|
@ -171,6 +170,37 @@ describe("m.request()", function () {
|
||||||
"CustomValue")
|
"CustomValue")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("overwrites existing headers", function () {
|
||||||
|
var error = m.prop()
|
||||||
|
|
||||||
|
m.request({
|
||||||
|
method: "POST",
|
||||||
|
url: "test",
|
||||||
|
// Trigger the Content-Type addition
|
||||||
|
data: {foo: "bar"},
|
||||||
|
headers: {
|
||||||
|
"Authorization" : "Bearer 12345abcd12345",
|
||||||
|
"CustomHeader" : "CustomValue",
|
||||||
|
"Content-Type" : "CustomType"
|
||||||
|
}
|
||||||
|
}).then(null, error)
|
||||||
|
|
||||||
|
var xhr = mock.XMLHttpRequest.$instances.pop()
|
||||||
|
xhr.onreadystatechange()
|
||||||
|
|
||||||
|
expect(xhr.$headers).to.have.property(
|
||||||
|
"Authorization",
|
||||||
|
"Bearer 12345abcd12345")
|
||||||
|
|
||||||
|
expect(xhr.$headers).to.have.property(
|
||||||
|
"CustomHeader",
|
||||||
|
"CustomValue")
|
||||||
|
|
||||||
|
expect(xhr.$headers).to.have.property(
|
||||||
|
"Content-Type",
|
||||||
|
"CustomType")
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
it("correctly sets initial value", function () {
|
it("correctly sets initial value", function () {
|
||||||
var prop = m.request({
|
var prop = m.request({
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue