【发布时间】:2019-03-20 06:14:28
【问题描述】:
假设我有一个商店,其中有一个组织列表,用户可以“选择”组织,然后将其存储在“过滤器”数组中。
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[] 更改后重新运行)?
【问题讨论】:
标签: javascript reactjs store mobx