Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/qbhy/data-model
https://github.com/qbhy/data-model
Last synced: 20 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/qbhy/data-model
- Owner: qbhy
- Created: 2018-09-27T10:04:14.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-11-18T04:15:16.000Z (about 6 years ago)
- Last Synced: 2024-01-04T15:49:15.275Z (12 months ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# data-model
js 简单的数据模型## 安装
在项目中执行 `npm install q-data-model --save` 或者 `yarn add q-data-model`## 使用
```javascript
import {
Data,
Model,
Paginate,
List,
} from 'q-data-model';import axios from 'axios';
// 定义 service
async function findUser(id) {
return axios.get('/api/user/' + id);
}async function queryUsers() {
return axios.get('/api/user');
}async function concernUser(id) {
return axios.post(`/api/user/{${id}/concern`);
}// 定义模型
class User extends Model {
concern(user){
return concernUser(user.get('id'))
}
}class UserList extends List {
constructor(props) {
super(props, new User());
}
anonymousCount = undefined;
getAnonymousCount() {
if(this.anonymousCount !== undefined){
return this.anonymousCount;
}
this.anonymousCount = 0;
for (let user of this.data) {
if(!user.has('name')){
this.anonymousCount++;
}
}
return this.anonymousCount;
}
}/**
* userData 类似:
{
id: 1,
avatar: 'avatar url',
name: 'qbhy',
gender: 1,
others fields
}
*/findUser(1).then(userData=>{
const user = new User(userData);
user.get('avatar');
user.get('name', '匿名用户');
if(user.has('show')){
user.set('show', true);
}
});queryUsers().then(paginate=>{
const userPaginate = new Paginate(paginate, new UserList());
// const userPaginate = new Paginate(paginate, new User());
const userList = userPaginate.list();
userList.getAnonymousCount();
userPaginate.hasNextPage()
userPaginate.currentPage()
userPaginate.perPage()
userPaginate.total()
})
```
> 更多高级使用和扩展功能可以在 [issues](https://github.com/qbhy/data-model/issues) 跟我讨论
> [email protected]