This repository was archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamplify.request.js
More file actions
250 lines (215 loc) · 6.31 KB
/
Copy pathamplify.request.js
File metadata and controls
250 lines (215 loc) · 6.31 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*!
* Amplify Request 1.0a1
*
* Copyright 2011 appendTo LLC. (http://appendto.com/team)
* Dual licensed under the MIT or GPL licenses.
* http://appendto.com/open-source-licenses
*
* http://amplifyjs.com
*/
(function( amplify, $, undefined ) {
amplify.request = function( resourceId, data, callback ) {
// default to an empty hash just so we can handle a missing resourceId
// in one place
var settings = resourceId || {};
if ( typeof settings === "string" ) {
if ( $.isFunction( data ) ) {
callback = data;
data = {};
}
settings = {
resourceId: resourceId,
data: data || {},
success: callback
};
}
var resource = amplify.request.resources[ settings.resourceId ],
success = settings.success || $.noop,
error = settings.error || $.noop;
settings.success = function( data, extra ) {
amplify.publish( "request.success", settings, data, extra );
amplify.publish( "request.complete", settings, data, extra );
success.apply( this, arguments );
};
settings.error = function( data, extra ) {
amplify.publish( "request.error", settings, data, extra );
amplify.publish( "request.complete", settings, data, extra );
error.apply( this, arguments );
};
if ( !resource ) {
if ( !settings.resourceId ) {
throw "amplify.request: no resourceId provided";
}
throw "amplify.request: unknown resourceId: " + settings.resourceId;
}
if ( amplify.publish( "request.before", settings ) ) {
amplify.request.resources[ settings.resourceId ]( settings );
}
};
$.extend( amplify.request, {
types: {},
resources: {},
define: function( resourceId, type, settings ) {
if ( typeof type === "string" ) {
if ( !( type in amplify.request.types ) ) {
throw "amplify.request.define: unknown type: " + type;
}
settings.resourceId = resourceId;
amplify.request.resources[ resourceId ] =
amplify.request.types[ type ]( settings );
} else {
// no pre-processor or settings for one-off types (don't invoke)
amplify.request.resources[ resourceId ] = type;
}
}
});
amplify.request.types.ajax = function( defnSettings ) {
defnSettings = $.extend({
type: "GET"
}, defnSettings );
return function( settings ) {
var url = defnSettings.url,
data = settings.data,
ajaxSettings,
regex;
if ( typeof data !== "string" ) {
data = $.extend( true, {}, defnSettings.data, data );
$.each( data, function( key, value ) {
regex = new RegExp( "{" + key + "}", "g");
if ( regex.test( url ) ) {
url = url.replace( regex, value );
delete data[ key ];
}
});
}
$.ajax($.extend( {}, defnSettings, {
url: url,
type: defnSettings.type,
data: data,
dataType: defnSettings.dataType,
success: function( data, status, xhr ) {
settings.success( data, xhr );
},
// data parameter is for custom overrides that proxy this function
error: function( xhr, status, error, data ) {
if ( data === undefined ) {
// TODO: add support for ajax errors with data
data = null;
}
settings.error( data, xhr );
},
beforeSend: function( xhr, ajaxSettings ) {
var ret = defnSettings.beforeSend ?
defnSettings.beforeSend.apply( this, arguments ) : true;
return ret && amplify.publish( "request.before.ajax",
defnSettings, settings, ajaxSettings, xhr );
}
}) );
};
};
var cache = amplify.request.cache = {
_key: function( resourceId, data ) {
var length = data.length,
i = 0,
checksum = chunk();
while ( i < length ) {
checksum ^= chunk();
}
function chunk() {
return data.charCodeAt( i++ ) << 24 |
data.charCodeAt( i++ ) << 16 |
data.charCodeAt( i++ ) << 8 |
data.charCodeAt( i++ ) << 0;
}
return "request-" + resourceId + "-" + checksum;
},
_default: (function() {
var memoryStore = {};
return function( resource, settings, ajaxSettings ) {
// data is already converted to a string by the time we get here
var cacheKey = cache._key( resource.resourceId, ajaxSettings.data ),
duration = resource.cache;
if ( cacheKey in memoryStore ) {
ajaxSettings.success( memoryStore[ cacheKey ] );
return false;
}
var success = ajaxSettings.success;
ajaxSettings.success = function( data ) {
memoryStore[ cacheKey ] = data;
if ( typeof duration === "number" ) {
setTimeout(function() {
delete memoryStore[ cacheKey ];
}, duration );
}
success.apply( this, arguments );
};
};
}())
};
if ( amplify.store ) {
$.each( amplify.store.types, function( type ) {
cache[ type ] = function( resource, settings, ajaxSettings ) {
var cacheKey = cache._key( resource.resourceId, ajaxSettings.data ),
cached = amplify.store[ type ]( cacheKey );
if ( cached ) {
ajaxSettings.success( cached );
return false;
}
var success = ajaxSettings.success;
ajaxSettings.success = function( data ) {
amplify.store[ type]( cacheKey, data );
success.apply( this, arguments );
};
};
});
cache.persist = cache[ amplify.store.type ];
}
amplify.subscribe( "request.before.ajax", function( resource ) {
var cacheType = resource.cache;
if ( cacheType ) {
return cache[ cacheType in cache ? cacheType : "_default" ]
.apply( this, arguments );
}
});
amplify.request.decoders = {
// http://labs.omniti.com/labs/jsend
jsend: function( data, status, xhr, success, error ) {
if ( data.status === "success" ) {
success( data.data );
} else if ( data.status === "fail" ) {
error( data.data, "fail" );
} else if ( data.status === "error" ) {
delete data.status;
error( data );
}
}
};
amplify.subscribe( "request.before.ajax", function( resource, settings, ajaxSettings ) {
var _success = ajaxSettings.success,
_error = ajaxSettings.error,
decoder = $.isFunction( resource.decoder )
? resource.decoder
: resource.decoder in amplify.request.decoders
? amplify.request.decoders[ resource.decoder ]
: amplify.request.decoders._default;
if ( !decoder ) {
return;
}
function success( xhr ) {
return function( data ) {
_success( data, "success", xhr );
};
}
function error( xhr ) {
return function( data ) {
_error( xhr, "error", null, data );
};
}
ajaxSettings.success = function( data, status, xhr ) {
decoder( data, status, xhr, success( xhr ), error( xhr ) );
};
ajaxSettings.error = function( xhr, status ) {
decoder( null, status, xhr, success( xhr ), error( xhr ) );
};
});
}( amplify, jQuery ) );