Sto trasferendo un po 'di codice javascript su dattiloscritto e ho riscontrato un problema.Typescript + Jquery Ajax + questo
Ho una chiamata AJAX che passa un oggetto come contesto, questo oggetto contiene alcuni callback e alcune altre informazioni che vengono lette dai callback di successo o di errore che indicano dove l'invocazione successo dovrebbe essere ridiretti su:
function SomeAjaxService
{
var self = this;
self.CheckErrorType = function(...) {
// Do something
};
var SuccessProxyCallback = function(results) {
// Do some stuff with results, like send off some events
this.successCallback(results);
};
var FailureProxyCallback = function(...) {
// Do some stuff with errors, and fire some events
var someErrorType = self.CheckErrorType(...);
this.failureCallback(someErrorType);
};
self.SendRequest = function(requestArgs) {
var ajaxSettings = {
context: requestArgs,
// Assign a load of stuff
};
$.ajax(ajaxSettings);
};
}
Ora, come si può vedere, il proxy di errore chiama un metodo locale e un oggetto di contesto "questo". Quindi, come puoi gestire questo tipo di scenario con dattiloscritto?
È sufficiente impostare self = this
nel costruttore e continuare come prima?
== == EDIT
Come richiesto qui è una rappresentazione classe di sopra mostra il problema di questo bisogno di essere 2 cose a causa della variabile self mancante.
class SomeAjaxService
{
public CheckErrorType(someArg1: any, someArg2: any) {
// Do some stuff with data
};
private SuccessProxyCallback(results: any) {
// Do some stuff with results, like send off some events
this.successCallback(results);
// Does the above *THIS* usage point to the context of the
// Ajax call or the actual instantiated class
};
private FailureProxyCallback(...) {
// Do some stuff with errors, and fire some events
var someErrorType = this.CheckErrorType(...);
// The above *THIS* call cannot be correct if the context has overwritten
// the scope of this, however I dont know if this is true, do I even need
// this.CheckErrorType to invoke a local method?
this.failureCallback(someErrorType);
// As mentioned above same issue here but the *THIS* should hopefully
// point to the context object on the ajax object not the local instance.
};
public SendRequest(requestArgs: any) {
var ajaxSettings : JqueryAjaxSettings = {
context: requestArgs,
// Assign a load of stuff
};
$.ajax(ajaxSettings);
};
}
Come ho detto in precedenza il problema principale è che in un metodo che ho bisogno di avvalersi di uno dei metodi di istanze, così come richiamare un metodo sull'oggetto contesto della chiamata AJAX. Con javascript raw avrei self = this
e quindi posso aggirare lo this
che è sovrascritto, che è necessario per mantenere l'oggetto async ajax nello scope.
La compilazione dattiloscritto di questo codice è proprio questo codice stesso, in modo da quello che hai davvero qui è una domanda JavaScript. O stai cercando di convertire questo in una classe (se sì, cosa hai finora)? –
Questo è il javascript originale, beh questo è un set mocked di javascript per mostrare il problema che ho. Aggiornerò con una classe di dattiloscritto se sarà d'aiuto, anche se ho pensato che il problema sarebbe stato evidente senza di esso. – Grofit
Questo potrebbe aiutare https://www.youtube.com/watch?v=tvocUcbCupA&hd=1 – basarat