I'm creating simple app, which will have few listeners. I can't figure out how logic behind that should look like.
HTML:
<div id="playerProgressBar">
<div id="playerHead"></div>
</div>
In Vue object i have defined variable:
music: document.getElementById('playerHead');
My listener should look like this:
music.addEventListener("timeupdate", timeUpdate, false);
music.addEventListener("canplaythrough", function () {
//code
}, false);
function timeUpdate() {
//code
}
So what would be correct way to use listeners in vuejs? Should i use custom directives here? Since I have no event I can't use methods. Putting whole logic in ready
function doesn't seem to be correct way. Thanks in advance!
This would be the Vue-esque way to assign HTML elements to the app:
This resolved to a variable
this.$els.videoElement
in the app. More info here.Now, to add listeners and functions for them, I'd do something like that:
Obviously, you could put everything video (or any other event related) stuff in a separate component (not directive, in Vue) to keep the code a bit more neat.
v-on (shorthand: @)
So, you could attach an event listener like this:
Here is an example where I'm using the MediaElement library: https://jsfiddle.net/248nqk02/