Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
jquery
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
jquery
Commits
19a0fdfb
Commit
19a0fdfb
authored
Sep 10, 2013
by
Timmy Willison
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Match ordering in ajax with master. -3 bytes.
parent
2f627494
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
155 additions
and
155 deletions
+155
-155
ajax.js
src/ajax.js
+155
-155
No files found.
src/ajax.js
View file @
19a0fdfb
...
@@ -136,12 +136,155 @@ function ajaxExtend( target, src ) {
...
@@ -136,12 +136,155 @@ function ajaxExtend( target, src ) {
}
}
// Attach a bunch of functions for handling common AJAX events
/* Handles responses to an ajax request:
jQuery
.
each
(
[
"ajaxStart"
,
"ajaxStop"
,
"ajaxComplete"
,
"ajaxError"
,
"ajaxSuccess"
,
"ajaxSend"
],
function
(
i
,
type
){
* - finds the right dataType (mediates between content-type and expected dataType)
jQuery
.
fn
[
type
]
=
function
(
fn
){
* - returns the corresponding response
return
this
.
on
(
type
,
fn
);
*/
};
function
ajaxHandleResponses
(
s
,
jqXHR
,
responses
)
{
});
var
firstDataType
,
ct
,
finalDataType
,
type
,
contents
=
s
.
contents
,
dataTypes
=
s
.
dataTypes
;
// Remove auto dataType and get content-type in the process
while
(
dataTypes
[
0
]
===
"*"
)
{
dataTypes
.
shift
();
if
(
ct
===
undefined
)
{
ct
=
s
.
mimeType
||
jqXHR
.
getResponseHeader
(
"Content-Type"
);
}
}
// Check if we're dealing with a known content-type
if
(
ct
)
{
for
(
type
in
contents
)
{
if
(
contents
[
type
]
&&
contents
[
type
].
test
(
ct
)
)
{
dataTypes
.
unshift
(
type
);
break
;
}
}
}
// Check to see if we have a response for the expected dataType
if
(
dataTypes
[
0
]
in
responses
)
{
finalDataType
=
dataTypes
[
0
];
}
else
{
// Try convertible dataTypes
for
(
type
in
responses
)
{
if
(
!
dataTypes
[
0
]
||
s
.
converters
[
type
+
" "
+
dataTypes
[
0
]
]
)
{
finalDataType
=
type
;
break
;
}
if
(
!
firstDataType
)
{
firstDataType
=
type
;
}
}
// Or just use first one
finalDataType
=
finalDataType
||
firstDataType
;
}
// If we found a dataType
// We add the dataType to the list if needed
// and return the corresponding response
if
(
finalDataType
)
{
if
(
finalDataType
!==
dataTypes
[
0
]
)
{
dataTypes
.
unshift
(
finalDataType
);
}
return
responses
[
finalDataType
];
}
}
/* Chain conversions given the request and the original response
* Also sets the responseXXX fields on the jqXHR instance
*/
function
ajaxConvert
(
s
,
response
,
jqXHR
,
isSuccess
)
{
var
conv2
,
current
,
conv
,
tmp
,
prev
,
converters
=
{},
// Work with a copy of dataTypes in case we need to modify it for conversion
dataTypes
=
s
.
dataTypes
.
slice
();
// Create converters map with lowercased keys
if
(
dataTypes
[
1
]
)
{
for
(
conv
in
s
.
converters
)
{
converters
[
conv
.
toLowerCase
()
]
=
s
.
converters
[
conv
];
}
}
current
=
dataTypes
.
shift
();
// Convert to each sequential dataType
while
(
current
)
{
if
(
s
.
responseFields
[
current
]
)
{
jqXHR
[
s
.
responseFields
[
current
]
]
=
response
;
}
// Apply the dataFilter if provided
if
(
!
prev
&&
isSuccess
&&
s
.
dataFilter
)
{
response
=
s
.
dataFilter
(
response
,
s
.
dataType
);
}
prev
=
current
;
current
=
dataTypes
.
shift
();
if
(
current
)
{
// There's only work to do if current dataType is non-auto
if
(
current
===
"*"
)
{
current
=
prev
;
// Convert response if prev dataType is non-auto and differs from current
}
else
if
(
prev
!==
"*"
&&
prev
!==
current
)
{
// Seek a direct converter
conv
=
converters
[
prev
+
" "
+
current
]
||
converters
[
"* "
+
current
];
// If none found, seek a pair
if
(
!
conv
)
{
for
(
conv2
in
converters
)
{
// If conv2 outputs current
tmp
=
conv2
.
split
(
" "
);
if
(
tmp
[
1
]
===
current
)
{
// If prev can be converted to accepted input
conv
=
converters
[
prev
+
" "
+
tmp
[
0
]
]
||
converters
[
"* "
+
tmp
[
0
]
];
if
(
conv
)
{
// Condense equivalence converters
if
(
conv
===
true
)
{
conv
=
converters
[
conv2
];
// Otherwise, insert the intermediate dataType
}
else
if
(
converters
[
conv2
]
!==
true
)
{
current
=
tmp
[
0
];
dataTypes
.
unshift
(
tmp
[
1
]
);
}
break
;
}
}
}
}
// Apply converter (if not an equivalence)
if
(
conv
!==
true
)
{
// Unless errors are allowed to bubble, catch and return them
if
(
conv
&&
s
[
"throws"
]
)
{
response
=
conv
(
response
);
}
else
{
try
{
response
=
conv
(
response
);
}
catch
(
e
)
{
return
{
state
:
"parsererror"
,
error
:
conv
?
e
:
"No conversion from "
+
prev
+
" to "
+
current
};
}
}
}
}
}
}
return
{
state
:
"success"
,
data
:
response
};
}
jQuery
.
extend
({
jQuery
.
extend
({
...
@@ -654,155 +797,12 @@ jQuery.each( [ "get", "post" ], function( i, method ) {
...
@@ -654,155 +797,12 @@ jQuery.each( [ "get", "post" ], function( i, method ) {
};
};
});
});
/* Handles responses to an ajax request:
// Attach a bunch of functions for handling common AJAX events
* - finds the right dataType (mediates between content-type and expected dataType)
jQuery
.
each
(
[
"ajaxStart"
,
"ajaxStop"
,
"ajaxComplete"
,
"ajaxError"
,
"ajaxSuccess"
,
"ajaxSend"
],
function
(
i
,
type
){
* - returns the corresponding response
jQuery
.
fn
[
type
]
=
function
(
fn
){
*/
return
this
.
on
(
type
,
fn
);
function
ajaxHandleResponses
(
s
,
jqXHR
,
responses
)
{
};
var
firstDataType
,
ct
,
finalDataType
,
type
,
});
contents
=
s
.
contents
,
dataTypes
=
s
.
dataTypes
;
// Remove auto dataType and get content-type in the process
while
(
dataTypes
[
0
]
===
"*"
)
{
dataTypes
.
shift
();
if
(
ct
===
undefined
)
{
ct
=
s
.
mimeType
||
jqXHR
.
getResponseHeader
(
"Content-Type"
);
}
}
// Check if we're dealing with a known content-type
if
(
ct
)
{
for
(
type
in
contents
)
{
if
(
contents
[
type
]
&&
contents
[
type
].
test
(
ct
)
)
{
dataTypes
.
unshift
(
type
);
break
;
}
}
}
// Check to see if we have a response for the expected dataType
if
(
dataTypes
[
0
]
in
responses
)
{
finalDataType
=
dataTypes
[
0
];
}
else
{
// Try convertible dataTypes
for
(
type
in
responses
)
{
if
(
!
dataTypes
[
0
]
||
s
.
converters
[
type
+
" "
+
dataTypes
[
0
]
]
)
{
finalDataType
=
type
;
break
;
}
if
(
!
firstDataType
)
{
firstDataType
=
type
;
}
}
// Or just use first one
finalDataType
=
finalDataType
||
firstDataType
;
}
// If we found a dataType
// We add the dataType to the list if needed
// and return the corresponding response
if
(
finalDataType
)
{
if
(
finalDataType
!==
dataTypes
[
0
]
)
{
dataTypes
.
unshift
(
finalDataType
);
}
return
responses
[
finalDataType
];
}
}
/* Chain conversions given the request and the original response
* Also sets the responseXXX fields on the jqXHR instance
*/
function
ajaxConvert
(
s
,
response
,
jqXHR
,
isSuccess
)
{
var
conv2
,
current
,
conv
,
tmp
,
prev
,
converters
=
{},
// Work with a copy of dataTypes in case we need to modify it for conversion
dataTypes
=
s
.
dataTypes
.
slice
();
// Create converters map with lowercased keys
if
(
dataTypes
[
1
]
)
{
for
(
conv
in
s
.
converters
)
{
converters
[
conv
.
toLowerCase
()
]
=
s
.
converters
[
conv
];
}
}
current
=
dataTypes
.
shift
();
// Convert to each sequential dataType
while
(
current
)
{
if
(
s
.
responseFields
[
current
]
)
{
jqXHR
[
s
.
responseFields
[
current
]
]
=
response
;
}
// Apply the dataFilter if provided
if
(
!
prev
&&
isSuccess
&&
s
.
dataFilter
)
{
response
=
s
.
dataFilter
(
response
,
s
.
dataType
);
}
prev
=
current
;
current
=
dataTypes
.
shift
();
if
(
current
)
{
// There's only work to do if current dataType is non-auto
if
(
current
===
"*"
)
{
current
=
prev
;
// Convert response if prev dataType is non-auto and differs from current
}
else
if
(
prev
!==
"*"
&&
prev
!==
current
)
{
// Seek a direct converter
conv
=
converters
[
prev
+
" "
+
current
]
||
converters
[
"* "
+
current
];
// If none found, seek a pair
if
(
!
conv
)
{
for
(
conv2
in
converters
)
{
// If conv2 outputs current
tmp
=
conv2
.
split
(
" "
);
if
(
tmp
[
1
]
===
current
)
{
// If prev can be converted to accepted input
conv
=
converters
[
prev
+
" "
+
tmp
[
0
]
]
||
converters
[
"* "
+
tmp
[
0
]
];
if
(
conv
)
{
// Condense equivalence converters
if
(
conv
===
true
)
{
conv
=
converters
[
conv2
];
// Otherwise, insert the intermediate dataType
}
else
if
(
converters
[
conv2
]
!==
true
)
{
current
=
tmp
[
0
];
dataTypes
.
unshift
(
tmp
[
1
]
);
}
break
;
}
}
}
}
// Apply converter (if not an equivalence)
if
(
conv
!==
true
)
{
// Unless errors are allowed to bubble, catch and return them
if
(
conv
&&
s
[
"throws"
]
)
{
response
=
conv
(
response
);
}
else
{
try
{
response
=
conv
(
response
);
}
catch
(
e
)
{
return
{
state
:
"parsererror"
,
error
:
conv
?
e
:
"No conversion from "
+
prev
+
" to "
+
current
};
}
}
}
}
}
}
return
{
state
:
"success"
,
data
:
response
};
}
return
jQuery
;
return
jQuery
;
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment