[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad tls-sprint
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Mon, 16 Feb 2009 20:40:12 +0100
branchtls-sprint
changeset 642 2cf7b79d8e77
parent 641 ed668804af37
child 643 616191014b8b
[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
web/data/cubicweb.ajax.js
--- a/web/data/cubicweb.ajax.js	Mon Feb 16 20:34:52 2009 +0100
+++ b/web/data/cubicweb.ajax.js	Mon Feb 16 20:40:12 2009 +0100
@@ -9,7 +9,18 @@
 
 var JSON_BASE_URL = baseuri() + 'json?';
 
-function postAjaxLoad(node) {
+/*
+ * inspect dom response, search for a <div class="ajaxHtmlHead"> node and
+ * put its content into the real document's head.
+ * This enables dynamic css and js loading and is used by replacePageChunk
+ */
+function loadAjaxHtmlHead(node) {
+    jQuery(node).find('div.ajaxHtmlHead').appendTo(jQuery('head'));
+}
+
+function postAjaxLoad(node, req) {
+    // addStylesheets(evalJSON(req.getResponseHeader('X-Cubicweb-Stylesheets') || '[]'));
+    loadAjaxHtmlHead(node);
     // find sortable tables if there are some
     if (typeof(Sortable) != 'undefined') {
 	Sortable.sortTables(node);
@@ -28,23 +39,18 @@
 
 // cubicweb loadxhtml plugin to make jquery handle xhtml response
 jQuery.fn.loadxhtml = function(url, data, reqtype, mode) {
-    var ajax = null;
-    if (reqtype == 'post') {
-	ajax = jQuery.post;
-    } else {
-	ajax = jQuery.get;
-    }
     if (this.size() > 1) {
 	log('loadxhtml was called with more than one element');
     }
+    var node = this.get(0); // only consider the first element
     mode = mode || 'replace';
     var callback = null;
     if (data && data.callback) {
 	callback = data.callback;
 	delete data.callback;
     }
-    var node = this.get(0); // only consider the first element
-    ajax(url, data, function(response) {
+    var deferred = loadJSON(url, data, reqtype);
+    deferred.addCallback(function(response, req) {
 	var domnode = getDomFromResponse(response);
 	if (mode == 'swap') {
 	    var origId = node.id;
@@ -57,7 +63,7 @@
 	} else if (mode == 'append') {
 	    jQuery(node).append(domnode);
 	}
-	postAjaxLoad(node);
+	postAjaxLoad(node, req);
 	while (jQuery.isFunction(callback)) {
 	    callback = callback.apply(this, [domnode]);
 	}