Skip to content

Commit

Permalink
MockJS tag escaption handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Bosn committed Apr 4, 2014
1 parent 6ce4550 commit 3cdf99c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ id|+1 @mock=100

在备注里,MOCK标签和普通的备注需要用分号隔开,比如一个参数叫userId,备注信息是"用户ID", mock标签是@mock=123,则在备注中应该填写:`用户ID;@mock=123`

#### MockJS模板的转义

变量名 备注 结果
escape @mock="123" "escape" : "\"123\""
noEscape @{mock}=[1,2,3] "noEscape" : [1,2,3]

明确不需要转义时,只需要写@{mock}即可

### for RD 之 Console ###

Expand Down
1 change: 1 addition & 0 deletions UPDATELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### rap v0.8.1 ###
* [BUG] 修复@行列 提出的MockJS模板转义问题。2014-04-04
* [BUG] 修复数据导入错误。 2014-04-02
* [功能] 增加pageTester对[action], {action]的URL语法的支持。 2014-04-02
* [BUG] 修复@行列 提出的KISSY complete的问题。 2014-03-27
Expand Down
2 changes: 1 addition & 1 deletion WebContent/demo/mock.plugin/index.htm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
url : url,
dataType : 'json',
type : 'get',
success : function(data) {
complete : function(data) {
console.log(data);
Node.one('#container').html(
JSON.stringify(data, null, 4));
Expand Down
14 changes: 14 additions & 0 deletions src/com/taobao/rigel/rap/mock/service/impl/MockMgrImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.taobao.rigel.rap.common.ArrayUtils;
import com.taobao.rigel.rap.common.MockjsRunner;
import com.taobao.rigel.rap.common.NumberUtils;
import com.taobao.rigel.rap.common.StringUtils;
import com.taobao.rigel.rap.mock.service.MockMgr;
import com.taobao.rigel.rap.project.bo.Action;
import com.taobao.rigel.rap.project.bo.Parameter;
Expand Down Expand Up @@ -601,17 +602,25 @@ private String mockValue(Parameter para, int index) {

private String mockjsValue(Parameter para, int index) {
String[] tags = para.getMockDataTEMP().split(";");
boolean escape = true;
Map<String, String> tagMap = new HashMap<String, String>();
parseTags(tags, tagMap, true);
String returnValue = "1";
String mockValue = tagMap.get("mock");
if (mockValue == null || mockValue.isEmpty()) {
mockValue = tagMap.get("{mock}");
escape = false;
}
if (mockValue != null && !mockValue.isEmpty()) {
if (mockValue.startsWith("[") && mockValue.endsWith("]")) {
return mockValue;
} else if (para.getDataType().equals("number")
|| para.getDataType().equals("boolean")) {
return mockValue;
} else {
if (escape) {
mockValue = StringUtils.escapeInJ(mockValue);
}
return "\"" + mockValue + "\"";
}

Expand Down Expand Up @@ -694,6 +703,11 @@ private void parseTags(String[] tags, Map<String, String> tagMap,
if (tagArr.length > 1) {
tagMap.put("mock", tagArr[1]);
}
} else if (tag.startsWith("@{mock}")) {
String[] tagArr = tag.split("=");
if (tagArr.length > 1) {
tagMap.put("{mock}", tagArr[1]);
}
}
}
}
Expand Down

0 comments on commit 3cdf99c

Please sign in to comment.