UNPKG

4.34 kBJavaScriptView Raw
1var Mock = require('./mock');
2var Util = require('./util')
3
4// BEGIN(BROWSER)
5
6function find(options) {
7
8 for (var sUrlType in Mock._mocked) {
9 var item = Mock._mocked[sUrlType]
10 if (
11 (!item.rurl || match(item.rurl, options.url)) &&
12 (!item.rtype || match(item.rtype, options.type.toLowerCase()))
13 ) {
14 // console.log('[mock]', options.url, '>', item.rurl)
15 return item
16 }
17 }
18
19 function match(expected, actual) {
20 if (Util.type(expected) === 'string') {
21 return expected === actual
22 }
23 if (Util.type(expected) === 'regexp') {
24 return expected.test(actual)
25 }
26 }
27
28}
29
30function convert(item, options) {
31 return Util.isFunction(item.template) ?
32 item.template(options) : Mock.mock(item.template)
33}
34
35/*
36 ### Mock.mockjax(library)
37
38 覆盖(拦截) Ajax 请求,目前内置支持 jQuery、Zepto、KISSY。
39
40*/
41// for jQuery
42Mock.mockjax = function mockjax(jQuery) {
43
44 function mockxhr() {
45 return {
46 readyState: 4,
47 status: 200,
48 statusText: '',
49 open: jQuery.noop,
50 send: function() {
51 this.onload()
52 },
53 setRequestHeader: jQuery.noop,
54 getAllResponseHeaders: jQuery.noop,
55 getResponseHeader: jQuery.noop,
56 statusCode: jQuery.noop,
57 abort: jQuery.noop
58 }
59 }
60
61 function prefilter(options, originalOptions, jqXHR) {
62 var item = find(options)
63 if (item) {
64 options.dataFilter =
65 options.converters['text json'] =
66 options.converters['text jsonp'] =
67 options.converters['text script'] =
68 options.converters['script json'] = function() {
69 return convert(item, options)
70 }
71 options.xhr = mockxhr
72
73 if (originalOptions.dataType !== 'script') return 'json'
74 }
75 }
76
77 // #22 步加载js文件的时候发现问题
78 // #23 Mock.mockjax 导致 $.getScript 不执行回调
79 jQuery.ajaxPrefilter('json jsonp script', prefilter)
80
81 return Mock
82}
83
84if (typeof jQuery != 'undefined') Mock.mockjax(jQuery)
85
86/*
87 for Zepto
88 因为 Zepto 并没有实现类似 jQuery.ajaxPrefilter 等预处理函数,所以将和 KISSY 类似直接粗暴处理。
89*/
90if (typeof Zepto != 'undefined') {
91 Mock.mockjax = function(Zepto) {
92 var __original_ajax = Zepto.ajax
93 var xhr = {
94 readyState: 4,
95 responseText: '',
96 responseXML: null,
97 state: 2,
98 status: 200,
99 statusText: 'success',
100 timeoutTimer: null
101 }
102
103 Zepto.ajax = function(options) {
104 var item = find(options)
105 if (item) {
106 var data = Mock.mock(item.template)
107 if (options.success) options.success(data, xhr, options)
108 if (options.complete) options.complete(xhr.status, xhr, options)
109 return xhr
110 }
111 return __original_ajax.call(Zepto, options)
112 }
113 }
114
115 Mock.mockjax(Zepto)
116}
117
118// for KISSY
119if (typeof KISSY != 'undefined' && KISSY.add) {
120 Mock.mockjax = function mockjax(KISSY) {
121 var _original_ajax = KISSY.io;
122
123 // @白汀 提交:此对象用于模拟 KISSY.io 响应之后的传给 success 方法的 xhr 对象,只构造了部分属性,不包含实际 KISSY 中的完整对象。
124 var xhr = {
125 readyState: 4,
126 responseText: '',
127 responseXML: null,
128 state: 2,
129 status: 200,
130 statusText: 'success',
131 timeoutTimer: null
132 };
133
134 KISSY.io = function(options) {
135 var item = find(options)
136 if (item) {
137 var data = Mock.mock(item.template)
138 if (options.success) options.success(data, xhr, options)
139 if (options.complete) options.complete(xhr.status, xhr, options)
140 return xhr
141 }
142 return _original_ajax.apply(this, arguments)
143 }
144
145 // 还原 KISSY.io 上的属性
146 for (var name in _original_ajax) {
147 KISSY.io[name] = _original_ajax[name]
148 }
149
150 }
151}
152// END(BROWSER)
\No newline at end of file