如何从其他商店功能更新商店

How to update a store from other store function

我有 2 个商店,即 AttachmentStore 和 CommentStore。

export class AttachmentStore {
  @observable attachments = []
}

export class CommentStore {
  @observable comments = []

  save_comment_and_attachment(comment, attachments) {
     this.comments.push(comment);
     //how can I push the attachment to the
     //attachments observable
  }

}

我知道我可以将附件声明为 CommentStore observable 的一部分,但是有什么方法可以更新从 CommentStore observable 的附件吗?

export class AttachmentStore {
  @observable attachments = [];

  @action add(attachments) {
    // ...
  }
}

export class CommentStore {
  @observable comments = [];
  attachmentStore = null;

  init(stores) {
    this.attachmentStore = stores.attachmentStore;
  }

  @action save_comment_and_attachment(comment, attachments) {
    this.comments.push(comment);
    this.attachmentStore.add(attachments);
  }
}

const stores = {
  attachmentStore: new AttachmentStore(),
  commentStore: new CommentStore(),
};
Object.values(stores)
  .filter(store => store.init)
  .forEach(store => store.init(stores));