Ali Rap+fiddler CGI data mock-up for app native app-----(ii) Add a mock rule that randomly returns data of type (different length) in 4

Source: Internet
Author: User
Tags switch case

# #RAP如何生成json数据 # #

1. Add mock rules to the page; direct storage;
2, the service obtains the corresponding rule string from the DB, invokes the Mock-min.js parsing, generates the data to return to the service;
Mockjsrunner.dorendermockjsrule calls Context.evaluatestring directly executes Mock-min.js, returning the corresponding generated results;
3, Service call callback function results returned to the front end;
# #RAP如何添加规则 # #
To extend the random.extend in Mock-min.js:

Random.extend ({/*randomly returns a random value that contains null, and the type includes String,number,boolean*/Testrandom:function(){ /*random Number Determination type*/varKey = Number (Math.floor (Math.random () *10) +2);/*2~36 Random length string*/varLength = Math.floor (Math.random () *36) +1;varresult;varFlag=0;if(Key>0 && Key <=3) {flag=1;}Else if(Key>3 && key<=6) {flag=2;}Else if(Key>6 && key<=9) {flag=3}Switch(flag) {/*return String Type*/ Case1: Result=math.random (). toString (length); Break;/*return Number Type*/ Case2:if(key==6) {result=0;}Else{ varType = key%2 = = 0? 1:-1; result=math.random () * MATH.POW (2,length) *type;} Break;/*return Boolean type*/ Case3: Result=key%2 = = 0?true:false; Break;/*return null*/default: Result=NULL;}returnresult;});


# # #遇到的问题记录 # #
1. In the switch case of JavaScript, case does not support expressions and only supports underlying type variables;
2, long time does not write the code, even the basic judgment expression all writes the mathematical expression, WTF;

Ali Rap+fiddler CGI data mock-up for app native app-----(ii) Add a mock rule that randomly returns data of type (different length) in 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.