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

能给一个使用BaseModel的案例吗? #2

Closed
softmaxs opened this issue Apr 25, 2022 · 1 comment · Fixed by #3
Closed

能给一个使用BaseModel的案例吗? #2

softmaxs opened this issue Apr 25, 2022 · 1 comment · Fixed by #3
Assignees
Labels
enhancement Some improvements

Comments

@softmaxs
Copy link

使用不来

@TechQuery TechQuery reopened this Apr 29, 2022
@TechQuery TechQuery added the enhancement Some improvements label Apr 29, 2022
@TechQuery TechQuery self-assigned this Jul 27, 2022
@TechQuery TechQuery linked a pull request Jul 27, 2022 that will close this issue
@TechQuery
Copy link
Member

@softmaxs 我基于我另一个 MobX 开源库写了个更简单的例子:

状态管理

import { stringify } from 'query-string';
import { ListModel } from 'mobx-restful';
import { client } from './service';
export type Repository = Record<'full_name' | 'html_url', string>;
export class RepositoryModel extends ListModel<Repository> {
client = client;
baseURI = 'orgs/idea2app/repos';
protected async loadPage(page: number, per_page: number) {
const { body } = await this.client.get<Repository[]>(
`${this.baseURI}?${stringify({ page, per_page })}`
);
return { pageData: body! };
}
}

页面组件

import { inject, observer } from 'mobx-react';
import { PureComponent } from 'react';
import { MainNav } from '../components/MainNav';
import { StoreProps } from '../store';
definePageConfig({
navigationBarTitleText: '请求接口'
});
@inject('store')
@observer
export default class InterfacePage extends PureComponent<StoreProps> {
componentDidMount() {
this.props.store.repositoryStore.getList();
}
componentWillUnmount() {
this.props.store.repositoryStore.clear();
}
render() {
const { currentPage } = this.props.store.repositoryStore;
return (
<>
<ul>
{currentPage.map(({ full_name, html_url }) => (
<li>
<a target="_blank" href={html_url}>
{full_name}
</a>
</li>
))}
</ul>
<MainNav path="interface" />
</>
);
}
}

@TechQuery TechQuery pinned this issue Aug 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Some improvements
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants