Domanda

Sto usando le raccomandazioni stabilite qui ( http://www.odetocode.com /articles/473.aspx ) per scrivere un sistema di webchat JavaScript AJAX utilizzando simulato namespacing e la prototipazione.

In uno dei miei metodi prototipo chiamo la $. Ajax metodo in jQuery . Quello che poi voglio fare è passare i dati JSON restituiti in un metodo dentro la mia JavaScript webchat namespace.

Il problema sembra essere dovuto al fatto che ho creato un'istanza della mia webchat JavaScript, non posso chiamare direttamente un metodo all'interno di esso perché ho bisogno di affrontare attraverso l'istanza.

La parte fondamentale nel seguente codice è

            success: function(data, textStatus) {
                this.GetUpdate_Success(data)
            },

Sto pensando perché siamo all'interno del metodo $ .ajax (), che questo non si riferisce più al nostro oggetto WebchatV3.

Il codice JavaScript completo è il seguente:

/// <reference path="/JavaScript/jquery-1.3.2-vsdoc2.js" />

// Simulated 'namespace'
var AvonAndSomerset = {}

// Chatroom run time data
AvonAndSomerset.WebchatV3 = function(memberId, passcode) {
    this.Members = new Array(); // Members in the chatroom
    this.Questions = new Array(); // The questions queue in the chatroom

// Details about the current user
this.currentMember = new AvonAndSomerset.WebchatV3.Member(memberId, passcode, null, null, null, null, null);

    // Set-up AJAX defaults
    $.ajaxSetup({ type: "POST", contentType: "application/json; charset=utf-8", dataType: "json" });
}

AvonAndSomerset.WebchatV3.prototype =
{
    // Get latest Member,Quetsion,Transcript and Room data from server
    GetUpdate: function(StartUp) {

        $.ajax({ url: "JSON.aspx/Members_GetChanges",
            data: "{ MemberID: " + this.currentMember.memberId + ", Passcode: \"" + this.currentMember.passcode + "\", ReturnAll: " + StartUp + " }",
            success: function(data, textStatus) {
                this.GetUpdate_Success(data)
            },
            error: function(result) {
                alert('Members_GetChanges() failed: ' + result.responseText);
            }
        });
    },
    // Callback - on success of GetUpdate()
    GetUpdate_Success: function(data) {
        alert('The AJAX call was successful!');
    },
    // Does the MemberID exist in the local array?
    Members_DoesExist: function(MemberID) {
        alert('Searching for ' + MemberID);

        alert(this.Members.length);
    }
È stato utile?

Soluzione

Il modo più semplice per risolvere questo problema è quello di creare una variabile che fa riferimento this alla giusta misura necessaria. this e lavorare portata in modo diverso in javascript poi molti linguaggi, in questo caso si riferisce all'oggetto che viene passato alla funzione.

// Get latest Member,Quetsion,Transcript and Room data from server
GetUpdate: function(StartUp) {
    //here
    var self = this;
    $.ajax({ url: "JSON.aspx/Members_GetChanges",
        data: "{ MemberID: " + this.currentMember.memberId + ", Passcode: \"" + this.currentMember.passcode + "\", ReturnAll: " + StartUp + " }",
        success: function(data, textStatus) {
            self.GetUpdate_Success(data)
        },
        error: function(result) {
            alert('Members_GetChanges() failed: ' + result.responseText);
        }
    });
},

Altri suggerimenti

Prova

        success: function(data, textStatus) {
            AvonAndSomerset.WebchatV3.GetUpdate_Success(data)
        },

che può funzionare.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top