一个包含位置以及检索或创建派生资源所需的其他参数的资源。它还提供了重试请求的能力。
| Name | Type | Description |
|---|---|---|
options |
string | Resource.ConstructorOptions | 一个 url 或描述初始化选项的对象 |
Example:
function refreshTokenRetryCallback(resource, error) {
if (error.statusCode === 403) {
// 403 status code means a new token should be generated
return getNewAccessToken()
.then(function(token) {
resource.queryParameters.access_token = token;
return true;
})
.catch(function() {
return false;
});
}
return false;
}
const resource = new Resource({
url: 'http://server.com/path/to/resource.json',
proxy: new DefaultProxy('/proxy/'),
headers: {
'X-My-Header': 'valueOfHeader'
},
queryParameters: {
'access_token': '123-435-456-000'
},
retryCallback: refreshTokenRetryCallback,
retryAttempts: 1
});
Members
static constant Cesium.Resource.DEFAULT : Resource
一个初始化为当前浏览器位置的资源实例
如果支持 blob 则返回 true。
资源的文件扩展名。
如果 Resource 具有请求头则为 true。这等同于检查 headers 属性是否有任何键。
将随请求一起发送的附加 HTTP 头。
如果 Resource 引用 blob URI 则为 true。
如果 Resource 引用跨域 URL 则为 true。
如果 Resource 引用 data URI 则为 true。
加载资源时要使用的代理。
附加到 url 的查询参数。
request : Request
将要使用的 Request 对象。仅供内部使用。
在放弃之前应调用 retryCallback 的次数。
当此资源的请求失败时调用的函数。如果它返回 true 或解析为 true 的 Promise,则请求将被重试。
用于替换 url 中模板参数的键值对。
已替换模板值、附加查询字符串并由代理(如果已设置)编码后的资源 url。
Methods
从 URL 创建 Resource 并对其调用 delete()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
string | object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
从 URL 创建 Resource 并对其调用 fetch()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
string | object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
创建 Resource 并对其调用 fetchArrayBuffer()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
string | object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
创建 Resource 并对其调用 fetchBlob()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
string | object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
创建 Resource 并对其调用 fetchImage()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
string | object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
创建 Resource 并对其调用 fetchJson()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
string | object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
从 URL 创建 Resource 并对其调用 fetchJsonp()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
string | object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
创建 Resource 并对其调用 fetchText()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
string | object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
创建 Resource 并对其调用 fetchXML()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
string | object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
从 URL 创建 Resource 并对其调用 head()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
string | object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
从 URL 创建 Resource 并对其调用 options()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
string | object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
从 URL 创建 Resource 并对其调用 patch()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
从 URL 创建 Resource 并对其调用 post()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
从 URL 创建 Resource 并对其调用 put()。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
A url or an object with the following properties
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
向 URL 附加一个正斜杠。
合并指定对象和现有的查询参数。这使你可以一次性添加多个参数,而不是逐个添加到 queryParameters 属性。
| Name | Type | Description |
|---|---|---|
params |
object | 查询参数 |
clone(result) → Resource
复制一个 Resource 实例。
| Name | Type | Description |
|---|---|---|
result |
Resource | optional 用于存储结果的对象。 |
Returns:
修改后的 result 参数,如果未提供则为新的 Resource 实例。
异步删除给定资源。返回一个在加载后解析的 promise
加载后返回结果,如果资源加载失败则拒绝。数据通过
XMLHttpRequest 加载,这意味着为了向其他源发出请求,
服务器必须启用跨域资源共享(CORS)头。
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
optional
Object with the following properties:
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
resource.delete()
.then(function(body) {
// use the data
}).catch(function(error) {
// an error occurred
});
See:
异步加载给定资源。返回一个在加载后解析的 promise
加载后返回结果,如果资源加载失败则拒绝。数据通过
XMLHttpRequest 加载,这意味着为了向其他源发出请求,
服务器必须启用跨域资源共享(CORS)头。 It's recommended that you use
the more specific functions eg. fetchJson, fetchBlob, etc.
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
optional
Object with the following properties:
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
resource.fetch()
.then(function(body) {
// use the data
}).catch(function(error) {
// an error occurred
});
See:
异步将资源作为原始二进制数据加载。返回一个在加载后解析的 promise
加载后返回 ArrayBuffer,如果资源加载失败则拒绝。数据通过
XMLHttpRequest 加载,这意味着为了向其他源发出请求,
服务器必须启用跨域资源共享(CORS)头。
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
// load a single URL asynchronously
resource.fetchArrayBuffer().then(function(arrayBuffer) {
// use the data
}).catch(function(error) {
// an error occurred
});
See:
异步将给定资源作为 blob 加载。返回一个在加载后解析的 promise
加载后返回 Blob,如果资源加载失败则拒绝。数据通过
XMLHttpRequest 加载,这意味着为了向其他源发出请求,
服务器必须启用跨域资源共享(CORS)头。
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
// load a single URL asynchronously
resource.fetchBlob().then(function(blob) {
// use the data
}).catch(function(error) {
// an error occurred
});
See:
异步加载给定的图像资源。返回一个在加载后解析为 ImageBitmap 的 promise(如果
preferImageBitmap 为 true 且浏览器支持 createImageBitmap),否则解析为 Image,如果图像加载失败则拒绝。
| Name | Type | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
optional
An object with the following properties.
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
// load a single image asynchronously
resource.fetchImage().then(function(image) {
// use the loaded image
}).catch(function(error) {
// an error occurred
});
// load several images in parallel
Promise.all([resource1.fetchImage(), resource2.fetchImage()]).then(function(images) {
// images is an array containing all the loaded images
});
See:
异步将给定资源作为 JSON 加载。返回一个在加载后解析的 promise
加载后返回 JSON 对象,如果资源加载失败则拒绝。数据通过
XMLHttpRequest 加载,这意味着为了向其他源发出请求,
服务器必须启用跨域资源共享(CORS)头。 如果尚未指定,此函数会将 'Accept: application/json,*/*;q=0.01' 添加到请求头。
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
resource.fetchJson().then(function(jsonData) {
// Do something with the JSON object
}).catch(function(error) {
// an error occurred
});
See:
使用 JSONP 请求资源。
| Name | Type | Default | Description |
|---|---|---|---|
callbackParameterName |
string |
'callback'
|
optional 服务器所期望的回调参数名称。 |
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
// load a data asynchronously
resource.fetchJsonp().then(function(data) {
// use the loaded data
}).catch(function(error) {
// an error occurred
});
See:
异步将给定资源作为文本加载。返回一个在加载后解析的 promise
加载后返回字符串,如果资源加载失败则拒绝。数据通过
XMLHttpRequest 加载,这意味着为了向其他源发出请求,
服务器必须启用跨域资源共享(CORS)头。
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
// load text from a URL, setting a custom header
const resource = new Resource({
url: 'http://someUrl.com/someJson.txt',
headers: {
'X-Custom-Header' : 'some value'
}
});
resource.fetchText().then(function(text) {
// Do something with the text
}).catch(function(error) {
// an error occurred
});
See:
异步将给定资源作为 XML 加载。返回一个在加载后解析的 promise
加载后返回 XML 文档,如果资源加载失败则拒绝。数据通过
XMLHttpRequest 加载,这意味着为了向其他源发出请求,
服务器必须启用跨域资源共享(CORS)头。
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
// load XML from a URL, setting a custom header
Cesium.loadXML('http://someUrl.com/someXML.xml', {
'X-Custom-Header' : 'some value'
}).then(function(document) {
// Do something with the document
}).catch(function(error) {
// an error occurred
});
See:
返回 Resource 的基础路径。
| Name | Type | Default | Description |
|---|---|---|---|
includeQuery |
boolean |
false
|
optional 是否将查询字符串与片段(fragment)以 URI 形式包含在内。 |
Returns:
资源的基础 URI
getDerivedResource(options) → Resource
返回一个相对于当前实例的资源。除非在 options 中被覆盖,否则所有属性与当前实例保持相同。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
An object with the following properties
|
Returns:
从当前资源派生的资源。
返回 url,可选包含查询字符串并由代理处理。
| Name | Type | Default | Description |
|---|---|---|---|
query |
boolean |
false
|
optional 如果为 true,则包含查询字符串。 |
proxy |
boolean |
false
|
optional 如果为 true,则 url 由代理对象处理(如果已定义)。 |
Returns:
包含所请求所有组件的 url。
异步获取给定资源的头信息。返回一个在加载后解析的 promise
加载后返回结果,如果资源加载失败则拒绝。数据通过
XMLHttpRequest 加载,这意味着为了向其他源发出请求,
服务器必须启用跨域资源共享(CORS)头。
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
optional
Object with the following properties:
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
resource.head()
.then(function(headers) {
// use the data
}).catch(function(error) {
// an error occurred
});
See:
异步获取给定资源的选项。返回一个在加载后解析的 promise
加载后返回结果,如果资源加载失败则拒绝。数据通过
XMLHttpRequest 加载,这意味着为了向其他源发出请求,
服务器必须启用跨域资源共享(CORS)头。
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
optional
Object with the following properties:
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
resource.options()
.then(function(headers) {
// use the data
}).catch(function(error) {
// an error occurred
});
See:
异步将数据修补到给定资源。返回一个在加载后解析的 promise
加载后返回结果,如果资源加载失败则拒绝。数据通过
XMLHttpRequest 加载,这意味着为了向其他源发出请求,
服务器必须启用跨域资源共享(CORS)头。
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
data |
object | 随资源一起发布的数据。 | ||||||||||||
options |
object |
optional
Object with the following properties:
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
resource.patch(data)
.then(function(result) {
// use the result
}).catch(function(error) {
// an error occurred
});
See:
异步将数据发布到给定资源。返回一个在加载后解析的 promise
加载后返回结果,如果资源加载失败则拒绝。数据通过
XMLHttpRequest 加载,这意味着为了向其他源发出请求,
服务器必须启用跨域资源共享(CORS)头。
| Name | Type | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
data |
object | 随资源一起发布的数据。 | |||||||||||||||
options |
object |
optional
Object with the following properties:
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
resource.post(data)
.then(function(result) {
// use the result
}).catch(function(error) {
// an error occurred
});
See:
异步将数据存放到给定资源。返回一个在加载后解析的 promise
加载后返回结果,如果资源加载失败则拒绝。数据通过
XMLHttpRequest 加载,这意味着为了向其他源发出请求,
服务器必须启用跨域资源共享(CORS)头。
| Name | Type | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
data |
object | 随资源一起发布的数据。 | ||||||||||||
options |
object |
optional
Object with the following properties:
|
Returns:
一个在加载后解析为所请求数据的 promise。如果
request.throttle 为 true 且请求优先级不够高,则返回 undefined。
Example:
resource.put(data)
.then(function(result) {
// use the result
}).catch(function(error) {
// an error occurred
});
See:
合并指定对象和现有的查询参数。这使你可以一次性添加多个参数,而不是逐个添加到 queryParameters 属性。如果某个值已设置,它将被新值替换。
| Name | Type | Default | Description |
|---|---|---|---|
params |
object | 查询参数 | |
useAsDefault |
boolean |
false
|
optional 如果为 true,参数将用作默认值,因此仅在不定义时才设置。 |
合并指定对象和现有的模板值。这使你可以一次性添加多个值,而不是逐个添加到 templateValues 属性。如果某个值已设置,它将变为数组并追加新值。
| Name | Type | Default | Description |
|---|---|---|---|
template |
object | 模板值 | |
useAsDefault |
boolean |
false
|
optional 如果为 true,值将用作默认值,因此仅在不定义时才设置。 |
重写 Object#toString,使隐式字符串转换返回此 Resource 所表示的完整 URL。
Returns:
此 Resource 所表示的 URL
Type Definitions
Resource 构造函数的初始化选项。
Properties:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
url |
string | 资源的 url。 | ||
queryParameters |
object |
<optional> |
包含检索资源时将发送的查询参数的对象。 | |
templateValues |
object |
<optional> |
用于替换模板值(例如 {x})的键值对。 | |
headers |
object |
<optional> |
{} | 将发送的附加 HTTP 头。 |
proxy |
Proxy |
<optional> |
加载资源时要使用的代理。 | |
retryCallback |
Resource.RetryCallback |
<optional> |
当此资源的请求失败时调用的 Function。如果返回 true,则请求将被重试。 | |
retryAttempts |
number |
<optional> |
0 | 在放弃之前应调用 retryCallback 的次数。 |
request |
Request |
<optional> |
将要使用的 Request 对象。仅供内部使用。 | |
parseUrl |
boolean |
<optional> |
true | 如果为 true,则解析 url 中的查询参数;否则按原样存储 url |
返回属性值的函数。
| Name | Type | Description |
|---|---|---|
resource |
Resource | optional 加载失败的资源。 |
error |
RequestErrorEvent | optional 资源加载过程中发生的错误。 |
Returns:
如果为 true 或解析为 true 的 promise,资源将被重试。否则将返回失败。
