php.de

Zurück   php.de > Webentwicklung > JavaScript, Ajax und mehr

JavaScript, Ajax und mehr dynamisches Scripten und Interaktion auf Clientebene

Antwort
 
LinkBack Themen-Optionen Thema bewerten
Alt 08.12.2008, 18:57  
Erfahrener Benutzer
 
Benutzerbild von tomtaz
 
Registriert seit: 17.06.2008
Beiträge: 1.762
PHP-Kenntnisse:
Fortgeschritten
tomtaz ist einfach richtig netttomtaz ist einfach richtig netttomtaz ist einfach richtig netttomtaz ist einfach richtig netttomtaz ist einfach richtig nett
tomtaz eine Nachricht über ICQ schicken tomtaz eine Nachricht über Skype™ schicken
Standard [Erledigt] Verliere Zuordung in einer JS Klasse

Hallo Leute,

ich experimentiere nun schon den ganzen Tag, aber ich komm nicht drauf.

Ich verliere in der Methode:
Code:
    /*
    ** Prepair Results
    */
    this.PrepairResults = function( response, status, statusText )
    {
        /*
        ** Hier verliere ich irgendwie die Zuordnung zur eigentlichen Klasse...
        ** das heist ich kann auf keien Eigenschaften oder Methoden der Klasse zugreifen, nur warum?
        */
        //alert( GetResults() );
        var res = response.getElementsByTagName( 'result' );

        //alert( obj.result );
        for ( var i = 0; i < res.length; i++ )
        {
            //alert( res[i].firstChild.nodeValue );
            this.result[i] = res[i].firstChild.nodeValue;
        }
        
        /*
        ** If we have Results
        */
        if ( this.result.length > 0 )
        {
            this.BuildMenu( );
            this.ShowMenu( );
        }
        else
        {
            this.HideMenu( );
        }
    }
die Zuordnung der Ursprungsklasse.
Aufgerufen wird die Methode durch einen Ajax Request
Code:
    /*
    ** Build Ajax Request
    */
    this.GetResults = function( )
    {
        //alert( 'GetResults' );
        var AjaxRequest = new Ajax( {
            Method       : 'GET',
            Url          : './suggest/',
            Params       : 'mode=xml&q=' + PHP.urlencode( this.value ),
            Async        : true,
            ResponseType : 'xml',
            Timeout      : 3000, // 3 Sekunden
            Scope        : this,
            
            
            OnError      : function( errno, errormsg )
            {
                alert( 'Error ' + errno + ' while loading the suggest response! - ' + errormsg );
            },
            
            OnSuccess    : this.PrepairResults,
            
            /*
            OnSuccess    : function( response, status, statusText )
            {
                //this.PrepairResults( response, status, statusText );
                alert( me.instance_name );
                self.result = response;
            }
            */
        } );
                
        /*
        ** do Request
        */
        AjaxRequest.doRequest( );
    }
Hier auch noch mal die Ajax Klasse:
Code:
function Ajax( settings )
{
    /*
    ** for later use the methods of this class
    */
    var self = this;
    
    /*
    ** Holds the Request Object
    */
    var xmlRequest = null;
    
    /*
    ** for the Timeout Handler
    */
    var timer     = null;
    var timeout   = null;
    
    /*
    ** Holds the given options
    */
    var options    = null;
    
    /*
    ** EventHandler
    */
    var HandleBevorSend = false;
    var HandleOnLoading = null;
    var HandleOnSuccess  = false;
    var HandleOnError   = false;
    
    /*
    ** Private
    */
    this.__construct = function( settings )
    {
        /*
        ** Validate given options
        */
        if ( typeof settings.Method == 'undefined' ) settings.Method = 'GET';
        if ( typeof settings.Url == 'undefined' ) settings.Method = location.href;
        if ( typeof settings.Params == 'undefined' ) settings.Params = null;
        if ( typeof settings.Async == 'undefined' ) settings.Async = true;
        if ( typeof settings.Timeout == 'undefined' ) settings.Timeout = null;
        if ( typeof settings.ResponseType == 'undefined' ) settings.ResponseType = 'xml';
        
        if ( typeof settings.OnLoading == 'undefined' )    settings.OnLoading = null;
        if ( typeof settings.OnSuccess == 'undefined' )    settings.OnSuccess = null;
        if ( typeof settings.OnError   == 'undefined' )    settings.OnError   = null;
        if ( typeof settings.OnTimeout == 'undefined' ) settings.OnTimeout = null;
        if ( typeof settings.Scope     == 'undefined' ) setting.Scope = null;
        
        this.HandleOnLoading = null;
        this.options = settings;
    }
    
    /*
    ** Public
    */
    this.doRequest = function( )
    {
        
        /*
        ** First we generate the XMLRequest object
        */
        if ( this.__CreateRequestObject( ) )
        {        
            /*
            ** Set Eventhandler
            */
            this.xmlRequest.onreadystatechange = this.__HandleResponse;

            /*
            ** Send an Post request
            */
            if ( this.options.Method == 'POST' )
            {
                this.xmlRequest.open( 'POST', this.options.Url, this.options.Async );
                
                this.xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                
                /*
                ** Send
                */
                this.xmlRequest.send( this.options.Params );
                
                
                return true;
            }
            
            /*
            ** Send an Get request
            */
            else if ( this.options.Method == 'GET' )
            {
                params = ( this.options.Params != null ) ? '?' + this.options.Params : '';
                
                this.xmlRequest.open( 'GET', this.options.Url + params, this.options.Async );
                
                /*
                ** Send
                */
                this.xmlRequest.send( null );
                
                return true;
            }
            
            /*
            ** Unsupportet RequestMode
            */
            else
            {
                alert ( 'Unsupportet Request Method' );
                return false;
            }
        }
        else
        {
            return false;
        }
    }
                
    /*
    ** Private
    */
    this.__HandleResponse = function( )
    {
        switch( self.xmlRequest.readyState )
        {
            
            /*
            ** On Loading...
            */
            case 1:
                //alert( 'Loading' );
                if ( self.HandleOnLoading == null )
                {
                    self.__HandleOnLoading( );
                }
                
                /*
                ** Now set timeout if we want
                */
                if ( self.options.Timeout != null && self.timer == null )
                {
                    self.timer = window.setTimeout
                    ( 
                        function( )
                        {
                            self.xmlRequest.onreadystatechange = function( ) { };
                            self.xmlRequest.abort();
                            self.xmlRequest = false;
                            self.__HandleOnTimeout( 408, 'Reqeuest Timeout' );
                        },
                        self.options.Timeout
                    );
                } // End timeout;
            break;
            
            /*
            ** Loading complete
            */
            case 4:
                
                /*
                ** Delete window.Timeout
                */
                if ( self.timer != null )
                {
                    window.clearTimeout( self.timer );
                }
                
                /*
                ** Check for right http statuscode
                */
                if ( self.xmlRequest.status !== 200 )
                {
                    self.__HandleOnError( self.xmlRequest.status, self.xmlRequest.statusText );
                }
                else
                {
                    /*
                    ** All looks like good. So return the needed informations
                    */
                    response = false;
                    switch( self.options.ResponseType )
                    {
                        /*
                        ** We give back an XML Dom Object
                        */
                        case 'xml':
                            if ( self.xmlRequest.responseXML != null )
                            {
                                response = self.xmlRequest.responseXML;
                            }
                            else
                            {
                                // fallback
                                response = self.xmlRequest.responseText;
                            }
                        break;
                        
                        /*
                        ** We give back an json Object
                        */
                        case 'json':
                            response = eval( '(' + self.xmlRequest.responseText + ')' );
                        break;
                        
                        /*
                        ** Default Output in Textmode
                        */
                        case 'text':
                        default:
                            response = self.xmlRequest.responseText;
                        break;
                    }
                    
                    self.__HandleOnSuccess( response, self.xmlRequest.status, self.xmlRequest.statusText );
                }
            break;
        } // End switch
    }

    this.__HandleOnLoading = function( )
    {
        if ( this.HandleOnLoading == null )
        {
            if ( typeof this.options.OnLoading == 'function' )
            {
                this.options.OnLoading( );
            }
            this.HandleOnLoading = true;
        }
    }
    
    this.__HandleOnTimeout = function( errno, errormsg )
    {
        if ( typeof this.options.OnTimeout == 'function' )
        {
            this.options.OnTimeout( errno, errormsg );
        }
    }
    
    this.__HandleOnError = function( errno, errormsg )
    {
        if ( typeof this.options.OnError == 'function' )
        {
            this.options.OnError( errno, errormsg );
        }
    }
    
    this.__HandleOnSuccess = function( response, status, statusText )
    {
        if ( typeof this.options.OnSuccess == 'function' )
        {
            this.options.OnSuccess( response, status, statusText );
        }
    }
    
    /*
    ** Private
    */
    this.__CreateRequestObject = function( )
    {
        try
        {
            /*
            ** Mozilla
            ** Opera
            ** Safari
            */
            this.xmlRequest = new XMLHttpRequest();
        }
        catch( e )
        {
            try
            {
                /*
                ** IE 5
                */
                this.xmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch ( e )
            {
                try
                {
                    /*
                    ** IE 6+
                    */
                    this.xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch ( e )
                {
                    alert( 'Your Browser does not support Ajax' );
                    return false;
                }
            }
        }
        
        return true;
    } // End function
    
    /*
    ** Call constructor method
    */
    this.__construct( settings );
}
Die Methode PrepairResults wird erfolgreich aufgreufen behinhaltet auch die ergebnise, heißt also, der Request wird erfolgreich abgesetzt, allerdings wie schon erwähnt, verliere ich in der Methode sämtliche Methoden und Eigenschaften. Vielleicht sieht ja einer direkt woran es happert.

Fals der Threadtitel nicht passt, so sagt es mir wie ich ihn ändern soll. Stehe mal wieder total aufn schlauch
__________________
Mfg Tomtaz
"Es soll jetzt diese Erfindung geben.... Kugel oder so heißt die. Ist so eine Art Suchmaschine..."
tomtaz ist offline   Mit Zitat antworten
Sponsor Mitteilung
PHP Code Flüsterer

Registriert seit: 21.08.2005
Beiträge: 4682
PHP-Kenntnisse:
Fortgeschritten

Alt 08.12.2008, 19:56  
Supermoderator HD
 
Benutzerbild von Manko10
 
Registriert seit: 16.03.2008
Beiträge: 8.425
PHP-Kenntnisse:
Fortgeschritten
Manko10 hat eine strahlende ZukunftManko10 hat eine strahlende ZukunftManko10 hat eine strahlende ZukunftManko10 hat eine strahlende ZukunftManko10 hat eine strahlende ZukunftManko10 hat eine strahlende ZukunftManko10 hat eine strahlende ZukunftManko10 hat eine strahlende ZukunftManko10 hat eine strahlende ZukunftManko10 hat eine strahlende ZukunftManko10 hat eine strahlende Zukunft
Standard

Ohne dass ich mir deinen Code jetzt großartige durchgelesen habe, vermute ich, dass du den this-Kontext durch die Asynchronität verlierst.
Um diese Problem zu vermeiden, habe ich schon einmal einen möglichen Lösungsweg vorgeschlagen: http://www.php.de/374441-post26.html
__________________
Refining Linux Advent Calendar series “24 Outstanding ZSH Gems
Manko10 ist offline   Mit Zitat antworten
Alt 08.12.2008, 21:55  
Erfahrener Benutzer
 
Benutzerbild von tomtaz
 
Registriert seit: 17.06.2008
Beiträge: 1.762
PHP-Kenntnisse:
Fortgeschritten
tomtaz ist einfach richtig netttomtaz ist einfach richtig netttomtaz ist einfach richtig netttomtaz ist einfach richtig netttomtaz ist einfach richtig nett
tomtaz eine Nachricht über ICQ schicken tomtaz eine Nachricht über Skype™ schicken
Standard

Danke Manko das war es. Ich hatte es zwar schon mal versucht glaub ich, aber irgendwie hab ich es dann doch hinbekommen. Vielen Dank.
__________________
Mfg Tomtaz
"Es soll jetzt diese Erfindung geben.... Kugel oder so heißt die. Ist so eine Art Suchmaschine..."
tomtaz ist offline   Mit Zitat antworten
Antwort


Themen-Optionen
Thema bewerten
Thema bewerten:

Forumregeln
Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are an
Gehe zu

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
Methode einer anderen Klasse aufrufen Luka PHP-Fortgeschrittene 15 09.11.2008 14:19
Klasse aus externer Klasse aufrufen kostja PHP Tipps 2008 8 07.08.2008 14:13
Variable aus Klasse herausbekommen GSJLink PHP Tipps 2008 7 16.02.2008 22:25
String-Parser Klasse - was muss rein? Matze PHP Tipps 2007 2 08.04.2007 22:14
Eine Klasse mehrere Dateien sn00py PHP Tipps 2006 6 08.05.2006 11:40
mehr als eine Klasse einbinden Alpha Centauri PHP-Fortgeschrittene 4 13.04.2006 20:56
Klasenname einer nicht instanziierten Klasse DerDesian PHP Tipps 2007 9 30.11.2005 13:13
Instanz einer Klasse in einer anderen Klasse verwenden Buhmann PHP-Fortgeschrittene 7 28.10.2005 23:12
[Erledigt] Führerschein für Klasse A+B.. Preis OK??? Off-Topic Diskussionen 20 13.07.2005 18:44
Rückgabewert auf einer Klasse anders als in der Klasse micbur PHP Tipps 2005-2 6 10.06.2005 15:06
Problem mit Übergabe einer Klasse in PHP4 PHP-Fortgeschrittene 10 08.01.2005 21:00
Klasse holt sich die Klasse PHP-Fortgeschrittene 9 07.10.2004 11:53
[Erledigt] Brauche Hilfe bei meiner ersten Klasse PHP-Fortgeschrittene 9 24.09.2004 17:09
Klassenobjet in anderer Klasse benutzen inu PHP Tipps 2004 6 19.09.2004 10:58
Klasse ändern UniQ PHP Tipps 2004 5 24.08.2004 14:46

Besucher kamen über folgende Suchanfragen bei Google auf diese Seite
if(typeof(this.value) == \undefined\ || !this.value.trim()), javascript \onsuccess\ event handler -prototype -mojoo -jquery, function ajaxsettings(), dorequest.mode=ie, javascript ajax json variable scope onsuccess

Alle Zeitangaben in WEZ +1. Es ist jetzt 19:48 Uhr.




Powered by vBulletin® Version 3.7.2 (Deutsch)
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
Aprilia-Forum, Aquaristik-Forum, Liebeskummer-Forum, Zierfisch-Forum, Geizkragen-Forum