Mobx.dart: Implementation of multiple stores (communication between stores)

Created on 28 Mar 2019  路  5Comments  路  Source: mobxjs/mobx.dart

How can i implement one parent store with multiple other stores, and use some observables in parent store and use some observable in store2 for example?

PARENT STORE:

import 'package:mobx/mobx.dart';
import 'store1.dart';
import 'store2.dart';

class ParentStore = ParentStoreBase with _$ParentStore;

abstract class ParentStoreBase implements Store {
@observable
  Store1 store1= new Store1();
@observable
  Store1 store2= new Store2();
@observable
  bool ParentLoading = false;
@action
  setParentLoading (bool value) {
    ParentLoading = value;
  }
}

STORE 2: (_how to communicate between stores or instantiate ParentStore to get to ParentStore observables/values?_)

import 'package:mobx/mobx.dart';
import 'parent_store.dart';

class Store2= Store2Base with _$Store2;

abstract class Store2Base implements Store {
@observable
  ParentStore parentStore= new ParentStore();
@observable
  bool value2= false;
@action
 setvalue2 (bool value) {
   if(parentStore.ParentLoading == true)
       value2 = value;
 }
}
discussion

Most helpful comment

Only showing the relevant parts:

// store2.dart
abstract class Store2Base implements Store {
  Store2Base({this.parent}); // store the parent

  final ParentStore parent;

  @observable
  bool value2= false;

  @action
  setvalue2 (bool value) {
    if(parent.ParentLoading == true)
      value2 = value;
  }
}

// parent.dart
abstract class ParentStoreBase implements Store {
  ParentStoreBase(){
    this.store1 = Store1(parent: this); // pass the parent
  }

  @observable
  Store1 store1;

  @observable
  bool ParentLoading = false;

  @action
  setParentLoading(bool value) {
    ParentLoading = value;
  }
}

All 5 comments

One quick option is to pass the ParentStore as constructor argument into your child stores

And how would passing ParentStore trough constructor look like in code? If you could give me an example?

And maybe you could add this too Examples for future users :)

Only showing the relevant parts:

// store2.dart
abstract class Store2Base implements Store {
  Store2Base({this.parent}); // store the parent

  final ParentStore parent;

  @observable
  bool value2= false;

  @action
  setvalue2 (bool value) {
    if(parent.ParentLoading == true)
      value2 = value;
  }
}

// parent.dart
abstract class ParentStoreBase implements Store {
  ParentStoreBase(){
    this.store1 = Store1(parent: this); // pass the parent
  }

  @observable
  Store1 store1;

  @observable
  bool ParentLoading = false;

  @action
  setParentLoading(bool value) {
    ParentLoading = value;
  }
}

Thanks for quick feedback, it works like charm!

@pavanpodila Doesn't that cause type errors? ParentStoreBase is not a ParentStore.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jiyarong picture jiyarong  路  4Comments

viktorszekeress picture viktorszekeress  路  4Comments

MaikuB picture MaikuB  路  3Comments

Tywholland picture Tywholland  路  3Comments

vishalchandra picture vishalchandra  路  6Comments