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

能否直接返回批量数组数据而不是一个对象? #6

Closed
wubrchina opened this issue Sep 12, 2013 · 2 comments
Closed

能否直接返回批量数组数据而不是一个对象? #6

wubrchina opened this issue Sep 12, 2013 · 2 comments
Assignees

Comments

@wubrchina
Copy link

因为项目居于backbone开发的,所以关于backbone.Collection的mock数据比较头疼。


[
    { ... },
    { ... }
    ...
]

目前mock似乎不支持上面数据结构的输出格式,只能手动一条一条添加,还是说我不知道?

想了下,可以换种方法:


var data = mock.mock({'data|5-10': [{ ... }]});
mock.mock(/url/, data.data);
@nuysoft
Copy link
Owner

nuysoft commented Sep 16, 2013

目前不支持直接返回数组。
你所描述的这种场景也挺常见,受你的启发,Mock.js 接下来会考虑要不要增加一个新的方法签名 Mock.mock(template, regulation),例如:

Mock.mock([{ ... }], '5-10')

在实现上,会接近于现在的内部方法 Handle.gen(template, name, obj),或者仅仅是把该内部方法暴露出来。

如果你有更合适的方法签名或实现,非常欢迎提供建议和贡献代码。

另外,参考下我最初的想法:
属性名 data|5-10 定义了生成数据时的规则,所以是必须的。从接口是否易读易用的角度看,如果希望直接传入数据模板 [{ ... }],那么,生成数据所需的规则势必需要通过其他参数传入,反而不如直接定义在属性名中直观。

@ghost ghost assigned nuysoft Sep 16, 2013
@nuysoft
Copy link
Owner

nuysoft commented Sep 22, 2013

Mock.mock(template, regulation)

由于 regulation 的格式和可选值太多,在使用这个方法签名时会导致混乱,目前不考虑支持。

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