stuartpb
2015-08-03
This is just a crazy idea I just had: what if we could get a promise for yet-to-be-parsed elements (or provide a callback)? Let’s say these functions would apply retroactively, so it could be called in async-loaded scripts as well as sync-loaded / inline scripts:
// awaitElementById, obviously, returns a Promise
awaitElementById('ticker').then(function(ticker) {
// hook up event listeners etc. here
});
// watchSelectorQuery, obviously, takes a callback, called multiple times
// an ES6 version returning an Observable could be specced later
watchSelectorQuery('.stock-symbol', function(stockSymbol) {
// again, hook up event listeners etc.
});
Would it be possible to polyfill this with Mutation Observers? (The big advantage to having it be available natively, of course, being not having to wait for the polyfill to be ready to write a script using it.)