Skip to content

新手问题 #131

@LS1231

Description

@LS1231

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
    }
  }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions