Added tests, fixed getScript and getJSON, fixed comment in $.ajax

This commit is contained in:
Jörn Zaefferer 2006-09-30 14:32:49 +00:00
parent 401b58c17e
commit 10532fb3ae

View file

@ -1,105 +1,121 @@
// 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
* been modified by the server. * been modified by the server.
* *
* @example $("#feeds").loadIfModified("feeds.html") * @example $("#feeds").loadIfModified("feeds.html")
* @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>
* *
* @name loadIfModified * @name loadIfModified
* @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.
* @param Hash params A set of key/value pairs that will be sent to the server. * @param Hash params A set of key/value pairs that will be sent to the server.
* @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.
* *
* @example $("#feeds").load("feeds.html") * @example $("#feeds").load("feeds.html")
* @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>
* *
* @name load * @example $("#feeds").load("feeds.html",
* @type jQuery * {test: true},
* @param String url The URL of the HTML file to load. * function() { alert("load is done"); }
* @param Hash params A set of key/value pairs that will be sent to the server. * );
* @param Function callback A function to be executed whenever the data is loaded. * @desc Same as above, but with an additional parameter
* @cat AJAX * and a callback that is executed when the data was loaded.
*/ *
jQuery.fn.load = function( url, params, callback, ifModified ) { * @test stop();
if ( url.constructor == Function ) * $('#first').load("data/name.php", function() {
return this.bind("load", url); * ok( $('#first').text() == 'ERROR', 'Check if content was injected into the DOM' );
* start();
callback = callback || function(){}; * });
*
// Default to a GET request * @name load
var type = "GET"; * @type jQuery
* @param String url The URL of the HTML file to load.
// If the second parameter was provided * @param Hash params A set of key/value pairs that will be sent to the server.
if ( params ) { * @param Function callback A function to be executed whenever the data is loaded.
// If it's a function * @cat AJAX
if ( params.constructor == Function ) { */
// We assume that it's the callback load: function( url, params, callback, ifModified ) {
callback = params; if ( url.constructor == Function )
params = null; return this.bind("load", url);
// Otherwise, build a param string callback = callback || function(){};
} else {
params = jQuery.param( params ); // Default to a GET request
type = "POST"; var type = "GET";
// If the second parameter was provided
if ( params ) {
// If it's a function
if ( params.constructor == Function ) {
// We assume that it's the callback
callback = params;
params = null;
// Otherwise, build a param string
} else {
params = jQuery.param( params );
type = "POST";
}
} }
var self = this;
// Request the remote document
jQuery.ajax( type, url, params,function(res, status){
if ( status == "success" || !ifModified && status == "notmodified" ) {
// Inject the HTML into all the matched elements
self.html(res.responseText).each( callback, [res.responseText, status] );
// Execute all the scripts inside of the newly-injected HTML
$("script", self).each(function(){
if ( this.src )
$.getScript( this.src );
else
eval.call( window, this.text || this.textContent || this.innerHTML || "" );
});
} else
callback.apply( self, [res.responseText, status] );
}, ifModified);
return this;
},
/**
* A function for serializing a set of input elements into
* a string of data.
*
* @example $("input[@type=text]").serialize();
* @before <input type='text' name='name' value='John'/>
* <input type='text' name='location' value='Boston'/>
* @after name=John&location=Boston
* @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
* @type String
* @cat AJAX
*/
serialize: function() {
return $.param( this );
} }
var self = this; });
// Request the remote document
jQuery.ajax( type, url, params,function(res, status){
if ( status == "success" || !ifModified && status == "notmodified" ) {
// Inject the HTML into all the matched elements
self.html(res.responseText).each( callback, [res.responseText, status] );
// Execute all the scripts inside of the newly-injected HTML
$("script", self).each(function(){
if ( this.src )
$.getScript( this.src );
else
eval.call( window, this.text || this.textContent || this.innerHTML || "" );
});
} else
callback.apply( self, [res.responseText, status] );
}, ifModified);
return this;
};
/**
* A function for serializing a set of input elements into
* a string of data.
*
* @example $("input[@type=text]").serialize();
* @before <input type='text' name='name' value='John'/>
* <input type='text' name='location' value='Boston'/>
* @after name=John&location=Boston
* @desc Serialize a selection of input elements to a string
*
* @name serialize
* @type String
* @cat AJAX
*/
jQuery.fn.serialize = function(){
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 ) {
jQuery.get(url, data, callback, "json"); if(callback)
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