Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion docs/core/asset-management-system.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ Given that CORS headers *are* set, `<a-assets>` will automatically set

## Preloading Audio and Video

Audio and video assets will only block the scene if `autoplay` is set or if `preload="auto"`:
Audio and video assets will only block the scene if `autoplay` is set or if
`preload="auto"`:

```html
<a-scene>
Expand Down Expand Up @@ -185,3 +186,17 @@ Thus, since we block entity initialization on assets, by the time entities
load, all assets will have been already fetched. As long as `<a-asset-item>`s
are defined, and the entity is fetching files using some form
`THREE.XHRLoader`, then caching will automatically work.

## Accessing the `XHRLoader` and Cache

To access the three.js `XHRLoader` if we want to listen more closely:

```js
console.log(document.querySelector('a-assets').fileLoader);
```

To access the cache that stores XHR responses:

```js
console.log(THREE.Cache);
```
5 changes: 3 additions & 2 deletions src/core/a-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var debug = require('../utils/debug');
var registerElement = require('./a-register-element').registerElement;
var THREE = require('../lib/three');

var xhrLoader = new THREE.XHRLoader();
var fileLoader = new THREE.XHRLoader();
var warn = debug('core:a-assets:warn');

/**
Expand All @@ -15,6 +15,7 @@ module.exports = registerElement('a-assets', {
createdCallback: {
value: function () {
this.isAssets = true;
this.fileLoader = fileLoader;
}
},

Expand Down Expand Up @@ -87,7 +88,7 @@ registerElement('a-asset-item', {
value: function () {
var self = this;
var src = this.getAttribute('src');
xhrLoader.load(src, function (textResponse) {
fileLoader.load(src, function (textResponse) {
THREE.Cache.files[src] = textResponse;
self.data = textResponse;
// Workaround for a Chrome bug.
Expand Down
14 changes: 14 additions & 0 deletions tests/core/a-assets.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* global assert, setup, suite, test */
var THREE = require('lib/three');

suite('a-assets', function () {
// Empty src will not trigger load events in Chrome. Use data URI where a load event is needed.
Expand All @@ -18,6 +19,19 @@ suite('a-assets', function () {
document.body.appendChild(scene);
});

test('throws error if not in a-scene', function () {
var div = document.createElement('div');
var assets = document.createElement('a-assets');
div.appendChild(assets);
assert.throws(function () {
assets.attachedCallback();
}, Error);
});

test('has fileLoader', function () {
assert.ok(this.el.fileLoader.constructor, THREE.XHRLoader);
});

test('waits for images to load', function (done) {
var el = this.el;
var scene = this.scene;
Expand Down