1 | var Mock = require('./mock');
|
2 | var Util = require('./util')
|
3 |
|
4 |
|
5 |
|
6 | function 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 |
|
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 |
|
30 | function convert(item, options) {
|
31 | return Util.isFunction(item.template) ?
|
32 | item.template(options) : Mock.mock(item.template)
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | Mock.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 |
|
78 |
|
79 | jQuery.ajaxPrefilter('json jsonp script', prefilter)
|
80 |
|
81 | return Mock
|
82 | }
|
83 |
|
84 | if (typeof jQuery != 'undefined') Mock.mockjax(jQuery)
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | if (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 |
|
119 | if (typeof KISSY != 'undefined' && KISSY.add) {
|
120 | Mock.mockjax = function mockjax(KISSY) {
|
121 | var _original_ajax = KISSY.io;
|
122 |
|
123 |
|
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 |
|
146 | for (var name in _original_ajax) {
|
147 | KISSY.io[name] = _original_ajax[name]
|
148 | }
|
149 |
|
150 | }
|
151 | }
|
152 |
|
\ | No newline at end of file |