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

不同层次字段之间的依赖的规则怎么写? #513

Open
jungleford opened this issue Dec 24, 2024 · 5 comments
Open

不同层次字段之间的依赖的规则怎么写? #513

jungleford opened this issue Dec 24, 2024 · 5 comments

Comments

@jungleford
Copy link

比如

const data = Mock.mock({
  gender: '@pick(["male", "female"])',
  name: {
    first: '@first',
    last: '@last',
    title: '???'
});

这里我需要title根据gender来填。
如果写成

    title: function () {
      return this.gender === 'male' ? 'Mr' : 'Ms';
    }

好像也不对,this只能取到同一层上的字段。
有什么规则可以做到吗?

@wll8
Copy link

wll8 commented Dec 25, 2024

@jacks-sam1010 别人问的是用户端的解决方案,结果你上来就给个 fork 源码修改方案?而且你 fork 就算了,使用示例、测试用例都没有,人工智能创造垃圾污染社区?

@wll8
Copy link

wll8 commented Dec 25, 2024

const data = util.libObj.mockjs.mock({
  gender: '@pick(["male", "female"])',
  id: '@id',
  name: {
    first: '@first',
    last: '@last',
    title(arg){
      const { gender } = arg.context.root
      return gender === 'male' ? 'Mr' : 'Ms';
    }
}})
console.log(`data`, data)
data {
  gender: 'female',
  id: '53250120200218657X',
  name: { first: 'Jennifer', last: 'Walker', title: 'Ms' }
}

提示:你可以直接使用生成数据的自动创建一系列增删改查的接口

@jungleford
Copy link
Author

const data = util.libObj.mockjs.mock({
  gender: '@pick(["male", "female"])',
  id: '@id',
  name: {
    first: '@first',
    last: '@last',
    title(arg){
      const { gender } = arg.context.root
      return gender === 'male' ? 'Mr' : 'Ms';
    }
}})
console.log(`data`, data)
data {
  gender: 'female',
  id: '53250120200218657X',
  name: { first: 'Jennifer', last: 'Walker', title: 'Ms' }
}

提示:你可以直接使用生成数据的自动创建一系列增删改查的接口

谢谢。
就是说还得装个mockm的第三方库来实现是吗?
mockjs自身有没有规则可以实现呢?

@wll8
Copy link

wll8 commented Dec 25, 2024

mockjs 本身就可以的,使用上面的函数参数中即可获取 root。我上面是说如果你需要额外功能的话可以使用 mockm。

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

3 participants
@wll8 @jungleford and others