This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Watch Vue.js v-models as Observable with $watchAsObservable and RxJS

    John LindquistJohn Lindquist

    You most likely already have data or properties in your template which are controlled by third-party components or updated using data binding. You can still use this data as stream by leveraging vue-rx's $watchAsObservable then chaining RxJS operators onto it as a new stream.

    vueVue.js
    ^2.0.0
    rxjsRxJS
    ^5.0.0
    javascriptJavaScript
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 00:00 You probably already have some components with some V models. For example, I'll create some data, and we'll return an active tab and set that to zero.

    00:12 Say you're using that as the V model of a tabs component, so this is the Bulma tabs component from Buefy, which has V tab items. V tab items have labels, so we'll set this label to Luke, that one to Darth, and that one to Leia, so Darth, Leia. Then set the V model here to active tab.

    00:36 Then just render out the active tab above the tab component. If I click Darth, this changes to one, and Leia, this changes to two. I can watch this V model value as an observable using this.$watchAsObservable, and then a string of active tab. We'll call this our active tab stream.

    01:05 We can return that and our object, active tab, and now render out our active tab stream. Now you'll notice there's no value there. If I click on Darth, it changes to an object of old value and new value. Leia, old one's Darth, new one's Leia, but if I refresh the page, that value disappears.

    01:30 We can trigger that to fire right away in here with some options. These are the same options as the $watch of view, and one of them is called immediate. We'll set that to true. That means to fire right away. You can see we get a new value of zero, and I didn't have to click anything. This still works, that still works, and that still works.

    01:53 You often don't have to worry about the old values. We can go ahead and pluck the new value, hit save, and now this zero up here is an observable stream, which updates whenever my component V model updates.

    Discuss

    Discuss