vuejs/vue-rx

dilemma between beforeCreate and created...

beeplin opened this issue · 2 comments

I realize that after the hook was changed from beforeCreate to created, now computed is initialized before subscriptions, so things in subscriptions cannot be used in computed. -_-~~

new Vue {
  subscriptions: {
    a: arrayFromSomeObservable;
  },
  computed: {
    b: function () {
      return this.a.length // not working.... coz this.a doesn't exist when this function runs for the first time
    }
  }
} 

A workaround is to change this.a.length to something like this.a === null? null : this.a.length (or this.a?.length in coffee). But don't seem neat....

Is there any way to overcome this while still being able to use this in subscriptions? puzzling...

That's smart! 👍
Did't release to npm?

Actually, I just noticed initializing in created does work with computed, because computed are lazy and not evaluated until render time. What you need is probably give the subscription an initial value with .startWith([])