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

mock 支持 #2

Closed
sorrycc opened this issue Dec 9, 2015 · 8 comments
Closed

mock 支持 #2

sorrycc opened this issue Dec 9, 2015 · 8 comments

Comments

@sorrycc
Copy link
Member

sorrycc commented Dec 9, 2015

基于 proxy.config.js,扩展类型为 plainObject 的情况,支持 Mock.js

使用方式如下:

var movie = {
  'id|+1': 1,
  'name': '@Name',
};

module.exports = {
  'GET /movies': {data|5: [movie], success: true},
  'GET /movie/:id': {data: movie, success: true},
}

请求 /movies,返回:

{
    "success": true,
    "data": [
        {
            "id": 3,
            "name": "Gary Wilson"
        },
        {
            "id": 2,
            "name": "Lisa Thomas"
        },
        {
            "id": 2,
            "name": "Ronald Lewis"
        },
        {
            "id": 3,
            "name": "Jason Robinson"
        },
        {
            "id": 5,
            "name": "Karen Lopez"
        }
    ]
}

请求 /movies/3,返回:

{
    "success": true,
    "data": {
        "id": 3,
        "name": "Melissa Moore"
    }
}
@sorrycc sorrycc mentioned this issue Dec 9, 2015
Closed
4 tasks
@sorrycc
Copy link
Member Author

sorrycc commented Dec 9, 2015

引用 mockjs 的 mock 方法可以直接引文件:

import {mock} from 'mockjs/src/mock';
mock(data)

@sorrycc
Copy link
Member Author

sorrycc commented Dec 9, 2015

其他的 mock 数据生成方案:

@soda-x
Copy link
Contributor

soda-x commented Dec 13, 2015

请求 /movies/3 ,返回:

{
    "success": true,
    "data": {
        "id": 3,
        "name": "Melissa Moore"
    }
}

对于这个的实现 目前考虑到有 几种实现方式

  • 保持既定
var movie = {
  'id|+1': 1,
  'name': '@Name',
};

但是在代理层在 使用 Mock.mock(tmp) 得到的 json 内部进行截获 并修改. 但是目前能想到的劣势很明显,首先需要在用户配置 proxy.config.js 时候去申请将来需要去截获并修改的字段 诸如 'GET /movie/:data.id': {data: movie, success: true} , 使用 data.id 的含义是申明截获的 object 路径,当然劣势还会有,当一个字段需要在返回数据里多处修改时,以及需要修改的字段在 一个 collection 内.

  • 使用 Mock.mock(rurl, function(option){}) 这种方式,即用 function 的方式
  • 遇到这种场景,直接推荐用户使用目前 mock 的 function 的方式,如果要支持数据模板,把 Mock.mock 传递进去即可.

@sorrycc
Copy link
Member Author

sorrycc commented Dec 14, 2015

请求 /movies/3 是否返回 id 为 3 的感觉不是很重要,可以不做处理,有需要让用户自己通过 function 或其他方式实现吧。

@soda-x
Copy link
Contributor

soda-x commented Dec 14, 2015

确定下 :
诸如 分页 场景'GET /api/:cat' : object,该方式只是 实现了 Mock.mock(tmpl) 的调用, 在分页场景下并不能适用. 因为 :cat 抑或 currentPageNumber 并不能传递进来

如果要实现上述分页需求 让用户 去用 function 实现 . 即 'GET /api/:cat' : function ,在这种模式下 function 内传入三个参数 req, callback ,Mock.mock (req 内会注入 params) ?

@soda-x
Copy link
Contributor

soda-x commented Dec 14, 2015

如果这样的话 'GET /api' : object 显得很单一 o.o

@sorrycc
Copy link
Member Author

sorrycc commented Dec 14, 2015

从使用的角度整理下看怎么用更好呢?

@soda-x
Copy link
Contributor

soda-x commented Dec 14, 2015

可以我去整理下 各种场景

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