Commit 10532fb3 authored by Jörn Zaefferer's avatar Jörn Zaefferer

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

parent 401b58c1
// 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.
*
* @example $("#feeds").load("feeds.html")
* @before <div id="feeds"></div>
* @result <div id="feeds"><b>45</b> feeds found.</div>
*
* @name load
* @type jQuery
* @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 Function callback A function to be executed whenever the data is loaded.
* @cat AJAX
*/
jQuery.fn.load = function( url, params, callback, ifModified ) {
if ( url.constructor == Function )
return this.bind("load", url);
callback = callback || function(){};
// Default to a GET request
var type = "GET";
// If the second parameter was provided /**
if ( params ) { * Load HTML from a remote file and inject it into the DOM.
// If it's a function *
if ( params.constructor == Function ) { * @example $("#feeds").load("feeds.html")
// We assume that it's the callback * @before <div id="feeds"></div>
callback = params; * @result <div id="feeds"><b>45</b> feeds found.</div>
params = null; *
* @example $("#feeds").load("feeds.html",
// Otherwise, build a param string * {test: true},
} else { * function() { alert("load is done"); }
params = jQuery.param( params ); * );
type = "POST"; * @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
* @type jQuery
* @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 Function callback A function to be executed whenever the data is loaded.
* @cat AJAX
*/
load: function( url, params, callback, ifModified ) {
if ( url.constructor == Function )
return this.bind("load", url);
callback = callback || function(){};
var self = this; // Default to a GET request
var type = "GET";
// Request the remote document // If the second parameter was provided
jQuery.ajax( type, url, params,function(res, status){ 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";
}
}
if ( status == "success" || !ifModified && status == "notmodified" ) { var self = this;
// Inject the HTML into all the matched elements
self.html(res.responseText).each( callback, [res.responseText, status] ); // Request the remote document
jQuery.ajax( type, url, params,function(res, status){
// Execute all the scripts inside of the newly-injected HTML if ( status == "success" || !ifModified && status == "notmodified" ) {
$("script", self).each(function(){ // Inject the HTML into all the matched elements
if ( this.src ) self.html(res.responseText).each( callback, [res.responseText, status] );
$.getScript( this.src );
else // Execute all the scripts inside of the newly-injected HTML
eval.call( window, this.text || this.textContent || this.innerHTML || "" ); $("script", self).each(function(){
}); if ( this.src )
} else $.getScript( this.src );
callback.apply( self, [res.responseText, status] ); else
eval.call( window, this.text || this.textContent || this.innerHTML || "" );
}, ifModified); });
} else
callback.apply( self, [res.responseText, status] );
return this; }, ifModified);
};
return this;
},
/** /**
* A function for serializing a set of input elements into * A function for serializing a set of input elements into
* a string of data. * a string of data.
* *
* @example $("input[@type=text]").serialize(); * @example $("input[@type=text]").serialize();
* @before <input type='text' name='name' value='John'/> * @before <input type='text' name='name' value='John'/>
* <input type='text' name='location' value='Boston'/> * <input type='text' name='location' value='Boston'/>
* @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
* *
* @name serialize * @test var data = $(':input').serialize();
* @type String * 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' );
* @cat AJAX *
*/ * @name serialize
jQuery.fn.serialize = function(){ * @type String
return $.param( this ); * @cat AJAX
}; */
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({ ...@@ -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({ ...@@ -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({ ...@@ -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({ ...@@ -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({ ...@@ -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({ ...@@ -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({ ...@@ -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({ ...@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment