HighDots Forums  

ajax in object kapseln

Javascript (German) Programmiersprache JavaScript. (de.comp.lang.javascript)


Discuss ajax in object kapseln in the Javascript (German) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
fiversen
 
Posts: n/a

Default ajax in object kapseln - 04-10-2007 , 11:55 AM






Hallo,

ich habe eine Web-Seite mit zig Ajax Anwendungen.
Nun wollte ich eine Klasse schreiben um die Dinge uebersichtlich zu
machen.

AjaxProc,
mit 'callProc', diese setzt die Member und sendet den Request;
mit 'calledProc', die aufgerufen wird - aber hier ist 'this.req'
leider undefined.

--------------------------------------------------------------------
//ajax
function AjaxProc()
{
this.req = null;
this.calledProc = calledProc;
this.callProc = callProc;
this.userProc = null;
}

function calledProc(req)
{
if (this.req.readyState == 4)
{
if (this.req.status == 200)
{
var result = this.req.responseText;
this.userProc(result);
req = 0;
}
}
}
function callProc(url)
{
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest)
{
this.req = new XMLHttpRequest();
alert(this.req);
this.req.onreadystatechange = this.calledProc;
this.req.open("GET", url, true);
this.req.send(null);
}
// branch for IE/Windows ActiveX version
else if (window.ActiveXObject)
{
this.req = new ActiveXObject("Microsoft.XMLHTTP");
alert(this.req);
if (this.req)
{
this.req.onreadystatechange = this.calledProc;
this.req.open("GET", url, true);
this.req.send();
}
}
}

var AjLoadTippe = null;
function loadTippe(spiel)
{
..
AjLoadTippe = new AjaxProc();
AjLoadTippe.userProc = processLoadTippe;
AjLoadTippe.callProc(url);
}
--------------------------------------------------------------------

Ich habe schon von aehnlichen Problemen gelesen - scheinbar laeuft die
automatisch aufgerufene Funktion
'calledProc' nicht im Kontext des Objects (wohl analog statischen
Funktionen in C++).
Nur wie stellt man dieses sicher.

Solong
Frank


Reply With Quote
  #2  
Old   
fiversen
 
Posts: n/a

Default Re: ajax in object kapseln - 04-12-2007 , 01:16 AM






Hier,
die Loesung mit closure.

------------------------------------------------------------
function AjaxProc()
{
var thisObj = this;
this.req = null;
this.calledProc = function() {
if (thisObj.req.readyState == 4)
{
if (thisObj.req.status == 200)
{
var result = thisObj.req.responseText;
//alert(result);
thisObj.userProc(result);
thisObj.req = 0;
}
}
}

this.callProc = function(url) {
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest)
{
thisObj.req = new XMLHttpRequest();
thisObj.req.onreadystatechange = thisObj.calledProc;
thisObj.req.open("GET", url, true);
thisObj.req.send(null);
}
// branch for IE/Windows ActiveX version
else if (window.ActiveXObject)
{
thisObj.req = new ActiveXObject("Microsoft.XMLHTTP");
if (thisObj.req)
{
thisObj.req.onreadystatechange = thisObj.calledProc;
thisObj.req.open("GET", url, true);
thisObj.req.send();
}
}
}

this.userProc = null;
}


var AjLoadTippe = null;
function loadTippe(spiel)
{
...
AjLoadTippe = new AjaxProc();
AjLoadTippe.userProc = processLoadTippe;
AjLoadTippe.callProc(url);
}

function processLoadTippe(result)
{
document.getElementById("tippI").innerHTML = result;
}
------------------------------------------------------------



Reply With Quote
  #3  
Old   
fiversen
 
Posts: n/a

Default Re: ajax in object kapseln - 04-12-2007 , 01:22 AM



Hier die Loesung mit closures.

------------------------------------------------------------
//Ajax mit closure
function AjaxProc()
{
var thisObj = this;
this.req = null;
this.calledProc = function() {
if (thisObj.req.readyState == 4)
{
if (thisObj.req.status == 200)
{
var result = thisObj.req.responseText;
//alert(result);
thisObj.userProc(result);
thisObj.req = 0;
}
}
}

this.callProc = function(url) {
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest)
{
thisObj.req = new XMLHttpRequest();
thisObj.req.onreadystatechange = thisObj.calledProc;
thisObj.req.open("GET", url, true);
thisObj.req.send(null);
}
// branch for IE/Windows ActiveX version
else if (window.ActiveXObject)
{
thisObj.req = new ActiveXObject("Microsoft.XMLHTTP");
if (thisObj.req)
{
thisObj.req.onreadystatechange = thisObj.calledProc;
thisObj.req.open("GET", url, true);
thisObj.req.send();
}
}
}

this.userProc = null;
}


var AjLoadTippe = null;
function loadTippe(spiel)
{
...
AjLoadTippe = new AjaxProc();
AjLoadTippe.userProc = processLoadTippe;
AjLoadTippe.callProc(url);
}
function processLoadTippe(result)
{
}
------------------------------------------------------------
Solong
Frank



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.