11

javascript コードを typescript に移植していますが、問題が発生しました。

オブジェクトをコンテキストとして渡す ajax 呼び出しがあります。このオブジェクトには、いくつかのコールバックと、成功の呼び出しをリダイレクトする場所を示す成功またはエラーのコールバックによって読み取られるその他の情報が含まれています。

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);
    };
}

ご覧のように、失敗したプロキシはローカル メソッドを呼び出し、「this」コンテキスト オブジェクトを使用します。では、この種のシナリオを typescript でどのように処理できるのでしょうか?

コンストラクターを設定self = thisして、以前と同じように続行できますか?

==編集==

ここで要求されているのは、上記のクラス表現で、self 変数が欠落しているためにこれが 2 つのものになる必要があるという問題を示しています。

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);
    };
}

上で述べたように、主な問題は、メソッドでインスタンス メソッドの 1 つを呼び出す必要があることと、ajax 呼び出しからコンテキスト オブジェクトでメソッドを呼び出す必要があることです。生のJavaScriptを使用すると、ajax非同期状態オブジェクトをスコープ内に保持するために必要なオーバーライドをself = this回避できます。this

4

1 に答える 1

8

コンストラクターで self = this を設定して、以前と同じように続行できますか?

抽象的に言えば、コンストラクターでこれを実行しても、通常は何も解決されません (後でそのメソッドでキャプチャしない限り、以下を参照してください)。TypeScript クラスは、ローカルをキャプチャするのではなく、オブジェクト自体にクラス インスタンス データを格納します。selfオブジェクトに保存されるためthis、以前にまだ持っていなかったものを持ってしまうことはありません。

これを行う1つの方法は次のとおりです。

class SomeAjaxService
{
    private FailureProxyCallback(context, arg) {
        // now 'this' is the class instance and 'context' is requestArgs 
    }

    public SendRequest(requestArgs) {
        var self = this; // Note: captured local used in closure below
        var ajaxSettings = {
            context: requestArgs,
            // Do not use an arrow function here, even though it's tempting!
            error: function(arg) { self.FailureProxyCallback(this, arg) }
        };

        $.ajax(ajaxSettings);
    };
}
于 2013-03-27T15:22:22.373 に答える