Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新手问题 #131

Open
LS1231 opened this issue Oct 10, 2021 · 1 comment
Open

新手问题 #131

LS1231 opened this issue Oct 10, 2021 · 1 comment

Comments

@LS1231
Copy link

LS1231 commented Oct 10, 2021

1、请求应该放在store中还是model中进行?
2、假如在页面的onLoad中,我需要根据store的数据进行一些判断(store里的数据是需要请求获取到的),然而用this.data.xxx获取不到,代码如下:

model

import api from '../api/index';

class Metadata {
  constructor(options) {
    this.dict = {};
    this.webSwitch = {};
    this.webConfig = {};
    this.isOpenSubstation = false;
    this.options = options;
  }

  async getMetadata() {
    const [{ data: dict }, { data: webConfig }, { data: webSwitch }] = await Promise.all([
      api.common.getDict(),
      api.common.getWebConfig(),
      api.common.getWebSwitch()
    ]);

    this.dict = dict;
    this.webSwitch = webSwitch;
    this.webConfig = webConfig;
    this.isOpenSubstation = Boolean(dict.SITE_LIST && dict.SITE_LIST.length > 0);
    this.options.onMetadataLoaded && this.options.onMetadataLoaded();
  }
}

export default Metadata;

store

import { Store } from 'westore';
import Metadata from '../models/metadata';
class MetadataStore extends Store {
  constructor(options) {
    super();
    this.options = options;
    this.data = {
      dict: {},
      webConfig: {},
      webSwitch: {},
      isOpenSubstation: false
    };

    this.metadata = new Metadata({
      onMetadataLoaded: () => {
        this.data.dict = this.metadata.dict;
        this.data.webConfig = this.metadata.webConfig;
        this.data.webSwitch = this.metadata.webSwitch;
        this.data.isOpenSubstation = this.metadata.isOpenSubstation;
        this.update();
      }
    });
  }

  getMetadata() {
    this.metadata.getMetadata();
  }
}

export default new MetadataStore();

view

import metadataStore from '../../stores/metadata-store';
Page({
  data: metadataStore.data,
  onLoad() {
    metadataStore.bind(this);
    metadataStore.getMetadata();
    // 这里获取不到webSitch
    if (this.data.webSwitch.near_jobs_status) {
      // do something
    }
  }
})

@dntzhang
Copy link
Collaborator

dntzhang commented Oct 11, 2021

1、请求应该放在store中还是model中进行?

我倾向于放到 store 中,model 只管数据注入,不管数据请求

2、假如在页面的onLoad中,我需要根据store的数据进行一些判断(store里的数据是需要请求获取到的),然而用this.data.xxx获取不到,

a. getMetadata 是异步所以取不到啊

  async onLoad() {
    metadataStore.bind(this);
    await metadataStore.getMetadata();
    // 这里获取不到webSitch
    if (this.data.webSwitch.near_jobs_status) {
      // do something
    }
  }

b.你不用到把逻辑写到 onLoad 里

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants