Mobx: How to alias an @observable on another class? @observable.alias a = other.a; ?

Created on 18 Sep 2019  ยท  3Comments  ยท  Source: mobxjs/mobx

  1. [x] Idea:

    • [x] What problem would it solve for you? It would allow me to transparently share annotated observables

    • [x] Do you think others will benefit from this change as well and it should in core package

    • [x] Are you willing to (attempt) a PR yourself?

      I have an Idea:

I have two State(store) classes with observable members. Both domains use a common set of data listHeaders. It seems to me that the best structure would be a third class with a listHeaderobservable, and a way to define observable aliases on my two State classes.

class A {
  @observable listHeaders = [];
}
class B {
  @observable.alias listHeaders = a.listHeaders;
  constructor(private a: A) {}
}
class C {
  @observable.alias listHeaders = a.listHeaders;
  constructor(private a: A) {}
}

You'd be able to assign listHeaders anywhere and fire a change everywhere since all three listHeaders are actually the same observable.

I haven't seen a way to do this. Perhaps I missed it.

โ” question

Most helpful comment

class C {  
  constructor(private a: A) {}
  get listHeaders() {
    return this.a.listHeaders;
  }
  set listHeaders(listHeaders) {
    return this.a.listHeaders = listHeaders;
  }
}

Why not to expose a on B/C?

All 3 comments

class C {  
  constructor(private a: A) {}
  get listHeaders() {
    return this.a.listHeaders;
  }
  set listHeaders(listHeaders) {
    return this.a.listHeaders = listHeaders;
  }
}

Why not to expose a on B/C?

closing as answered

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mdebbar picture mdebbar  ยท  3Comments

cafreeman picture cafreeman  ยท  4Comments

kmalakoff picture kmalakoff  ยท  4Comments

kirhim picture kirhim  ยท  3Comments

Niryo picture Niryo  ยท  3Comments