# #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