Added tests, fixed getScript and getJSON, fixed comment in $.ajax
This commit is contained in:
parent
401b58c17e
commit
10532fb3ae
120
src/ajax/ajax.js
120
src/ajax/ajax.js
|
@ -1,6 +1,4 @@
|
||||||
// AJAX Plugin
|
jQuery.fn.extend({
|
||||||
// Docs Here:
|
|
||||||
// http://jquery.com/docs/ajax/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load HTML from a remote file and inject it into the DOM, only if it's
|
* Load HTML from a remote file and inject it into the DOM, only if it's
|
||||||
|
@ -17,9 +15,9 @@
|
||||||
* @param Function callback A function to be executed whenever the data is loaded.
|
* @param Function callback A function to be executed whenever the data is loaded.
|
||||||
* @cat AJAX
|
* @cat AJAX
|
||||||
*/
|
*/
|
||||||
jQuery.fn.loadIfModified = function( url, params, callback ) {
|
loadIfModified: function( url, params, callback ) {
|
||||||
this.load( url, params, callback, 1 );
|
this.load( url, params, callback, 1 );
|
||||||
};
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load HTML from a remote file and inject it into the DOM.
|
* Load HTML from a remote file and inject it into the DOM.
|
||||||
|
@ -28,6 +26,19 @@ jQuery.fn.loadIfModified = function( url, params, callback ) {
|
||||||
* @before <div id="feeds"></div>
|
* @before <div id="feeds"></div>
|
||||||
* @result <div id="feeds"><b>45</b> feeds found.</div>
|
* @result <div id="feeds"><b>45</b> feeds found.</div>
|
||||||
*
|
*
|
||||||
|
* @example $("#feeds").load("feeds.html",
|
||||||
|
* {test: true},
|
||||||
|
* function() { alert("load is done"); }
|
||||||
|
* );
|
||||||
|
* @desc Same as above, but with an additional parameter
|
||||||
|
* and a callback that is executed when the data was loaded.
|
||||||
|
*
|
||||||
|
* @test stop();
|
||||||
|
* $('#first').load("data/name.php", function() {
|
||||||
|
* ok( $('#first').text() == 'ERROR', 'Check if content was injected into the DOM' );
|
||||||
|
* start();
|
||||||
|
* });
|
||||||
|
*
|
||||||
* @name load
|
* @name load
|
||||||
* @type jQuery
|
* @type jQuery
|
||||||
* @param String url The URL of the HTML file to load.
|
* @param String url The URL of the HTML file to load.
|
||||||
|
@ -35,7 +46,7 @@ jQuery.fn.loadIfModified = function( url, params, callback ) {
|
||||||
* @param Function callback A function to be executed whenever the data is loaded.
|
* @param Function callback A function to be executed whenever the data is loaded.
|
||||||
* @cat AJAX
|
* @cat AJAX
|
||||||
*/
|
*/
|
||||||
jQuery.fn.load = function( url, params, callback, ifModified ) {
|
load: function( url, params, callback, ifModified ) {
|
||||||
if ( url.constructor == Function )
|
if ( url.constructor == Function )
|
||||||
return this.bind("load", url);
|
return this.bind("load", url);
|
||||||
|
|
||||||
|
@ -81,7 +92,7 @@ jQuery.fn.load = function( url, params, callback, ifModified ) {
|
||||||
}, ifModified);
|
}, ifModified);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A function for serializing a set of input elements into
|
* A function for serializing a set of input elements into
|
||||||
|
@ -93,13 +104,18 @@ jQuery.fn.load = function( url, params, callback, ifModified ) {
|
||||||
* @after name=John&location=Boston
|
* @after name=John&location=Boston
|
||||||
* @desc Serialize a selection of input elements to a string
|
* @desc Serialize a selection of input elements to a string
|
||||||
*
|
*
|
||||||
|
* @test var data = $(':input').serialize();
|
||||||
|
* ok( data == 'action=Test&text2=Test&radio1=on&radio2=on&check=on&=on&hidden=&foo[bar]=&name=name&button=&=foobar&select1=&select2=3&select3=1', 'Check form serialization as query string' );
|
||||||
|
*
|
||||||
* @name serialize
|
* @name serialize
|
||||||
* @type String
|
* @type String
|
||||||
* @cat AJAX
|
* @cat AJAX
|
||||||
*/
|
*/
|
||||||
jQuery.fn.serialize = function(){
|
serialize: function() {
|
||||||
return $.param( this );
|
return $.param( this );
|
||||||
};
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
// If IE is used, create a wrapper for the XMLHttpRequest object
|
// If IE is used, create a wrapper for the XMLHttpRequest object
|
||||||
if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
|
if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
|
||||||
|
@ -291,6 +307,11 @@ jQuery.extend({
|
||||||
* alert("Script loaded and executed.");
|
* alert("Script loaded and executed.");
|
||||||
* })
|
* })
|
||||||
*
|
*
|
||||||
|
* @test stop();
|
||||||
|
* $.getScript("data/test.js", function() {
|
||||||
|
* ok( foobar == "bar", 'Check if script was evaluated' );
|
||||||
|
* start();
|
||||||
|
* });
|
||||||
*
|
*
|
||||||
* @name $.getScript
|
* @name $.getScript
|
||||||
* @type jQuery
|
* @type jQuery
|
||||||
|
@ -298,8 +319,8 @@ jQuery.extend({
|
||||||
* @param Function callback A function to be executed whenever the data is loaded.
|
* @param Function callback A function to be executed whenever the data is loaded.
|
||||||
* @cat AJAX
|
* @cat AJAX
|
||||||
*/
|
*/
|
||||||
getScript: function( url, data, callback ) {
|
getScript: function( url, callback ) {
|
||||||
jQuery.get(url, data, callback, "script");
|
jQuery.get(url, callback, "script");
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -317,6 +338,21 @@ jQuery.extend({
|
||||||
* }
|
* }
|
||||||
* )
|
* )
|
||||||
*
|
*
|
||||||
|
* @test stop();
|
||||||
|
* $.getJSON("data/json.php", {json: "array"}, function(json) {
|
||||||
|
* ok( json[0].name == 'John', 'Check JSON: first, name' );
|
||||||
|
* ok( json[0].age == 21, 'Check JSON: first, age' );
|
||||||
|
* ok( json[1].name == 'Peter', 'Check JSON: second, name' );
|
||||||
|
* ok( json[1].age == 25, 'Check JSON: second, age' );
|
||||||
|
* start();
|
||||||
|
* });
|
||||||
|
* @test stop();
|
||||||
|
* $.getJSON("data/json.php", function(json) {
|
||||||
|
* ok( json.data.lang == 'en', 'Check JSON: lang' );
|
||||||
|
* ok( json.data.length == 25, 'Check JSON: length' );
|
||||||
|
* start();
|
||||||
|
* });
|
||||||
|
*
|
||||||
* @name $.getJSON
|
* @name $.getJSON
|
||||||
* @type jQuery
|
* @type jQuery
|
||||||
* @param String url The URL of the page to load.
|
* @param String url The URL of the page to load.
|
||||||
|
@ -325,7 +361,11 @@ jQuery.extend({
|
||||||
* @cat AJAX
|
* @cat AJAX
|
||||||
*/
|
*/
|
||||||
getJSON: function( url, data, callback ) {
|
getJSON: function( url, data, callback ) {
|
||||||
|
if(callback)
|
||||||
jQuery.get(url, data, callback, "json");
|
jQuery.get(url, data, callback, "json");
|
||||||
|
else {
|
||||||
|
jQuery.get(url, data, "json");
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -347,6 +387,15 @@ jQuery.extend({
|
||||||
* }
|
* }
|
||||||
* )
|
* )
|
||||||
*
|
*
|
||||||
|
* @test stop();
|
||||||
|
* $.post("data/name.php", {xml: "5-2"}, function(xml){
|
||||||
|
* $('math', xml).each(function() {
|
||||||
|
* ok( $('calculation', this).text() == '5-2', 'Check for XML' );
|
||||||
|
* ok( $('result', this).text() == '3', 'Check for XML' );
|
||||||
|
* });
|
||||||
|
* start();
|
||||||
|
* });
|
||||||
|
*
|
||||||
* @name $.post
|
* @name $.post
|
||||||
* @type jQuery
|
* @type jQuery
|
||||||
* @param String url The URL of the page to load.
|
* @param String url The URL of the page to load.
|
||||||
|
@ -372,6 +421,32 @@ jQuery.extend({
|
||||||
* @example $.ajaxTimeout( 5000 );
|
* @example $.ajaxTimeout( 5000 );
|
||||||
* @desc Make all AJAX requests timeout after 5 seconds.
|
* @desc Make all AJAX requests timeout after 5 seconds.
|
||||||
*
|
*
|
||||||
|
* @test stop();
|
||||||
|
* var passed = 0;
|
||||||
|
* var timeout;
|
||||||
|
* $.ajaxTimeout(1000);
|
||||||
|
* var pass = function() {
|
||||||
|
* passed++;
|
||||||
|
* if(passed == 2) {
|
||||||
|
* ok( true, 'Check local and global callbacks after timeout' );
|
||||||
|
* clearTimeout(timeout);
|
||||||
|
* start();
|
||||||
|
* }
|
||||||
|
* };
|
||||||
|
* var fail = function(ba) {
|
||||||
|
* console.debug(ba);
|
||||||
|
* ok( false, 'Check for timeout failed' );
|
||||||
|
* start();
|
||||||
|
* };
|
||||||
|
* timeout = setTimeout(fail, 1500);
|
||||||
|
* $('#main').ajaxError(pass);
|
||||||
|
* $.ajax({
|
||||||
|
* type: "GET",
|
||||||
|
* url: "data/name.php?wait=5",
|
||||||
|
* error: pass,
|
||||||
|
* success: fail
|
||||||
|
* });
|
||||||
|
*
|
||||||
* @name $.ajaxTimeout
|
* @name $.ajaxTimeout
|
||||||
* @type jQuery
|
* @type jQuery
|
||||||
* @param Number time How long before an AJAX request times out.
|
* @param Number time How long before an AJAX request times out.
|
||||||
|
@ -429,6 +504,27 @@ jQuery.extend({
|
||||||
* });
|
* });
|
||||||
* @desc Save some data to the server and notify the user once its complete.
|
* @desc Save some data to the server and notify the user once its complete.
|
||||||
*
|
*
|
||||||
|
* @test stop();
|
||||||
|
* $.ajax({
|
||||||
|
* type: "GET",
|
||||||
|
* url: "data/name.php?name=foo",
|
||||||
|
* success: function(msg){
|
||||||
|
* ok( msg == 'bar', 'Check for GET' );
|
||||||
|
* start();
|
||||||
|
* }
|
||||||
|
* });
|
||||||
|
*
|
||||||
|
* @test stop();
|
||||||
|
* $.ajax({
|
||||||
|
* type: "POST",
|
||||||
|
* url: "data/name.php",
|
||||||
|
* data: "name=peter",
|
||||||
|
* success: function(msg){
|
||||||
|
* ok( msg == 'pan', 'Check for POST' );
|
||||||
|
* start();
|
||||||
|
* }
|
||||||
|
* });
|
||||||
|
*
|
||||||
* @name $.ajax
|
* @name $.ajax
|
||||||
* @type jQuery
|
* @type jQuery
|
||||||
* @param Hash prop A set of properties to initialize the request with.
|
* @param Hash prop A set of properties to initialize the request with.
|
||||||
|
@ -468,7 +564,7 @@ jQuery.extend({
|
||||||
xml.setRequestHeader("If-Modified-Since",
|
xml.setRequestHeader("If-Modified-Since",
|
||||||
jQuery.lastModified[url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
|
jQuery.lastModified[url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
|
||||||
|
|
||||||
// Set header so calling script knows that it's an XMLHttpRequest
|
// Set header so the called script knows that it's an XMLHttpRequest
|
||||||
xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
||||||
|
|
||||||
// Make sure the browser sends the right content length
|
// Make sure the browser sends the right content length
|
||||||
|
|
Loading…
Reference in a new issue