Вы можете использовать события вместо таймера - генерировать событие, когда предварительно загруженный материал сделан:
_preload: function() {
//do some work here
emit('preloaded_done')
this.preloaded = true;
},
exposed:function(){
if (!this.preloaded) {
this.el.addEventListener('preloaded_done', this.exposed)
return;
}
// all things that are needed to be done after the preload
}
Таким образом, открытая функция будет выполнена после того, как предварительно загруженный материал будет сделан.
Или вы можете хранить события в массиве
init: function() {
// ... binds and stuff
this.cachedEvents = []
this.el.addEventListener('loaded', this.cacheEvents)
stuffBeforeEmittingEvents()
},
cacheEvents: function(e) {
this.cachedEvents.push(e)
this.el.removeEventListener(e.type, this.cacheEvents)
e.stopPropagation()
}
и, как только вы сделаете свои вещи, просто выполните цикл и испустите их
for(i = 0; i < this.cachedEvents.length; i++) {
this.el.emit(this.cachedEvents[i].type, this.cachedEvents[i])
}
Что-то вродеэто:
init: function() {
this.events = []
this.stuff = this.stuff.bind(this);
this.cacheEvents = this.cacheEvents.bind(this);
this.emitCachedEvents = this.emitCachedEvents.bind(this);
this.el.addEventListener('loaded', this.cacheEvents)
this.stuff()
},
stuff: function() {
// whatever you need to do
this.emitCachedEvents()
},
cacheEvents(e) {
this.events.push(e)
// prevent bubbling + catching the same event
this.el.removeEventListener(e.type, this.cacheEvents)
e.stopPropagation()
},
emitCachedEvents() {
for (let i = 0; i < this.events.length; i++) {
// primitive, may require polyfill - emit(name, data)
emit(this.events[i].type, this.events[i])
}
}
Проверьте это в моей скрипке .