⚠️ This lesson is retired and might contain outdated information.

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

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 9 months ago

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.

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.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today