Ankündigung

Einklappen
Keine Ankündigung bisher.

[Erledigt] Verliere Zuordung in einer JS Klasse

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • [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
    [B]Mfg Tomtaz[/B]
    [I]"Es soll jetzt diese Erfindung geben.... Kugel oder so heißt die. Ist so eine Art Suchmaschine..." [/I]:!::shock:

  • #2
    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: “[url=http://www.refining-linux.org/archives/65/Performing-push-backups-Part-1-rdiff-backup/]Performing Push Backups – Part 1: rdiff-backup[/url]”

    Kommentar


    • #3
      Danke Manko das war es. Ich hatte es zwar schon mal versucht glaub ich, aber irgendwie hab ich es dann doch hinbekommen. Vielen Dank.
      [B]Mfg Tomtaz[/B]
      [I]"Es soll jetzt diese Erfindung geben.... Kugel oder so heißt die. Ist so eine Art Suchmaschine..." [/I]:!::shock:

      Kommentar

      Lädt...
      X