util.js
4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
define([
'exports',
'../errors/RequestError',
'../errors/CancelError',
'../Deferred',
'../io-query',
'../_base/array',
'../_base/lang',
'../promise/Promise'
], function(exports, RequestError, CancelError, Deferred, ioQuery, array, lang, Promise){
exports.deepCopy = function deepCopy(target, source){
for(var name in source){
var tval = target[name],
sval = source[name];
if(tval !== sval){
if(tval && typeof tval === 'object' && sval && typeof sval === 'object'){
exports.deepCopy(tval, sval);
}else{
target[name] = sval;
}
}
}
return target;
};
exports.deepCreate = function deepCreate(source, properties){
properties = properties || {};
var target = lang.delegate(source),
name, value;
for(name in source){
value = source[name];
if(value && typeof value === 'object'){
target[name] = exports.deepCreate(value, properties[name]);
}
}
return exports.deepCopy(target, properties);
};
var freeze = Object.freeze || function(obj){ return obj; };
function okHandler(response){
return freeze(response);
}
function dataHandler (response) {
return response.data || response.text;
}
exports.deferred = function deferred(response, cancel, isValid, isReady, handleResponse, last){
var def = new Deferred(function(reason){
cancel && cancel(def, response);
if(!reason || !(reason instanceof RequestError) && !(reason instanceof CancelError)){
return new CancelError('Request canceled', response);
}
return reason;
});
def.response = response;
def.isValid = isValid;
def.isReady = isReady;
def.handleResponse = handleResponse;
function errHandler(error){
error.response = response;
throw error;
}
var responsePromise = def.then(okHandler).otherwise(errHandler);
if(exports.notify){
responsePromise.then(
lang.hitch(exports.notify, 'emit', 'load'),
lang.hitch(exports.notify, 'emit', 'error')
);
}
var dataPromise = responsePromise.then(dataHandler);
// http://bugs.dojotoolkit.org/ticket/16794
// The following works around a leak in IE9 through the
// prototype using lang.delegate on dataPromise and
// assigning the result a property with a reference to
// responsePromise.
var promise = new Promise();
for (var prop in dataPromise) {
if (dataPromise.hasOwnProperty(prop)) {
promise[prop] = dataPromise[prop];
}
}
promise.response = responsePromise;
freeze(promise);
// End leak fix
if(last){
def.then(function(response){
last.call(def, response);
}, function(error){
last.call(def, response, error);
});
}
def.promise = promise;
def.then = promise.then;
return def;
};
exports.addCommonMethods = function addCommonMethods(provider, methods){
array.forEach(methods||['GET', 'POST', 'PUT', 'DELETE'], function(method){
provider[(method === 'DELETE' ? 'DEL' : method).toLowerCase()] = function(url, options){
options = lang.delegate(options||{});
options.method = method;
return provider(url, options);
};
});
};
exports.parseArgs = function parseArgs(url, options, skipData){
var data = options.data,
query = options.query;
if(data && !skipData){
if(typeof data === 'object'){
options.data = ioQuery.objectToQuery(data);
}
}
if(query){
if(typeof query === 'object'){
query = ioQuery.objectToQuery(query);
}
if(options.preventCache){
query += (query ? '&' : '') + 'request.preventCache=' + (+(new Date));
}
}else if(options.preventCache){
query = 'request.preventCache=' + (+(new Date));
}
if(url && query){
url += (~url.indexOf('?') ? '&' : '?') + query;
}
return {
url: url,
options: options,
getHeader: function(headerName){ return null; }
};
};
exports.checkStatus = function(stat){
stat = stat || 0;
return (stat >= 200 && stat < 300) || // allow any 2XX response code
stat === 304 || // or, get it out of the cache
stat === 1223 || // or, Internet Explorer mangled the status code
!stat; // or, we're Titanium/browser chrome/chrome extension requesting a local file
};
});