2017-05-18 13 views
0

私は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の観測の一部として添付ファイルを宣言することができます知っているが、私はCommentStoreから観察可能な添付ファイルを更新することができます任意の方法はありますか?

答えて

1
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)); 
+0

偉大な答え。私はそれを更新したい店に店を渡すことによって同じ答えを見つけました。 – rechie

関連する問題