Last active
August 14, 2017 09:26
-
-
Save yannan/44e098d7a3a1bbdce4c3d6e16e07558b to your computer and use it in GitHub Desktop.
mock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from 'axios'; | |
| import MockAdapter from 'axios-mock-adapter'; | |
| import Mock from 'mockjs'; | |
| import { | |
| Todos | |
| } from './data/todoList'; // 导入Todos数据 | |
| export default { | |
| /** | |
| * mock start | |
| */ | |
| start() { // 初始化函数 | |
| let mock = new MockAdapter(axios); // 创建 MockAdapter 实例 | |
| // 获取todo列表 | |
| mock.onGet('/todo/list').reply(config => { // config 指 前台传过来的值 | |
| let mockTodo = Todos.map(tode => { // 重组 Todos数组,变成我们想要的数据 | |
| return { | |
| id: tode.id, | |
| title: tode.title, | |
| count: tode.record.filter((data) => { | |
| if (data.checked === false) return true; | |
| return false; | |
| }).length, // 过滤到record里面 ‘checked’ 为true的数据,因为它们已经被完成了 | |
| locked: tode.locked, | |
| isDelete: tode.isDelete | |
| }; | |
| }).filter(tode => { | |
| if (tode.isDelete === true) return false; // 过滤掉 ‘isDelete’为true,因为已经被删除了。 | |
| return true; | |
| }); | |
| return new Promise((resolve, reject) => { | |
| setTimeout(() => { | |
| resolve([200, { | |
| todos: mockTodo // 返回状态为200,并且返回todos数据 | |
| }]); | |
| }, 200); | |
| }); | |
| }); | |
| // 新增一条todo | |
| mock.onPost('/todo/addTodo').reply(config => { | |
| Todos.push({ | |
| id: Mock.Random.guid(), | |
| title: 'newList', | |
| isDelete: false, | |
| locked: false, | |
| record: [] | |
| }); | |
| return new Promise((resolve, reject) => { | |
| setTimeout(() => { | |
| resolve([200]); | |
| }, 200); | |
| }); | |
| }); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment