javascript - 在 mobX 中,可以将 "listen"存储到另一个存储,并在另一个存储更改时执行查询吗?

标签 javascript reactjs store mobx

假设我有一个商店,里面有一个组织列表,用户可以“选择”组织,然后将这些组织存储在“过滤器”数组中。

export class OrganizationStore extends ArrayStore {
    //organizations = new Map();

    constructor( ...args) {
        super(args);
        this.organizations = new Map();
        this.filter = [];
        this.getter_url = '/organization/get-organizations';
    }

    async getData() {
        const full_url = new URL(this.getter_url);
        const options = {};
        options.method = 'GET';
        options.credentials = process.env.NODE_ENV === 'production' ? 'same-origin' : 'include';

        const response = await fetch(full_url, options);
        if (response.ok) {
            const d = await response.json();
            this.buildStore(d);
        }

    }

    buildStore(values) {
        this.organizations.clear();
        for (const {id, name} of values) {
            this.organizations.set(id, new Organization(id, name));
        }
    }

    get count() {
        return this.organizations.size;
    }
}

decorate(OrganizationStore, {
    organizations: observable,
    filter: observable,
    count: computed,
});



export class Organization {
    constructor(id, name) {
        this.id = id;
        this.name = name;
    }
}

还有一家商店

export class UserStore extends ArrayStore {
    constructor(organizationStore, ...args) {
        super(args);
        this.users = [];
        this.getter_url = '/users/get-users';
        this.organizationStore = organizationStore;
    }

    async getData() {
        const full_url = new URL(this.getter_url);
        const options = {};
        options.method = 'GET';
        options.credentials = process.env.NODE_ENV === 'production' ? 'same-origin' : 'include';

        query = {filter: this.organizationStore.filter()};
        //how to make this line "observe" the original store

        Object.keys(query).forEach(key => full_url.searchParams.append(key, options.query[key]));


        const response = await fetch(full_url, options);
        if (response.ok) {
            const d = await response.json();
            this.buildStore(d);
        }


    }
}

现在(如何)我可以让商店自动刷新自己(让 getData 在 organizationStore.filter[] 更改后重新运行)?

最佳答案

我认为您可能正在寻找 reactions .

Reaction - A variation on autorun that gives more fine grained control on which observables will be tracked. It takes two functions, the first one (the data function) is tracked and returns data that is used as input for the second one, the effect function.

export class UserStore extends ArrayStore {
    constructor(organizationStore, ...args) {
        super(args);
        this.users = [];
        this.getter_url = '/users/get-users';
        this.organizationStore = organizationStore;

        // Dispose of this when done with it
        const disposer = reaction(
            () => this.organizationStore.filter.length,
            () => this.getData()
        );
    }

    // ...
}

另一个选项,如果你想要事件更多的控制,是使用 observe

const disposer = observe(this.organizationStore.filter, (change) => {
    // Change is an object with a couple properties that describe what has changed and how
});

但我认为 react 对你来说没问题。

关于javascript - 在 mobX 中,可以将 "listen"存储到另一个存储,并在另一个存储更改时执行查询吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52823149/

相关文章:

javascript - 启用仅单击一个子 jquery

javascript - 在不使用状态和 Action 的情况下同步两个 react 组件的滚动

javascript - 从 json store extjs 获取记录

hadoop - 使用Pig存储在Hbase中时出错

javascript - 伪造 Canvas ——这可行吗?

javascript - 使用 javascript 将 ckEditor 中的选定内容替换为新内容

javascript - vue条件显示

reactjs - 如何为 Flow 中嵌套在 HOC 中的组件输入 props?

javascript - props.items.map 不是使用 React 的函数

java - 在 Android 上存储和恢复非原始数据的最佳方式?