如何將對象的副本傳遞給$ .ajax的.done()函數?

[英]How can I pass copies of object to the function in the .done() of a $.ajax?


I have the following code:

我有以下代碼:

function doDialogAjax(link: Link. modal: Modal) {
    $.ajax( link.Url,
    {
        cache: false,
        dataType: 'html'
    })
        .done(onDialogDone)
        .fail(onDialogFail);
}

function onDialogDone(data: any, textStatus: string, jqXHR: JQueryXHR) {
    var x = data;
    // I need to access link.abc and modal.def properties here
}

How can I send the link object to my onDialogDone() function? I seem to remember there is some way to send specify context information objects but I can't find any examples of this.

如何將鏈接對象發送到我的onDialogDone()函數?我似乎記得有一些方法來發送指定上下文信息對象,但我找不到任何這樣的例子。

2 个解决方案

#1


2  

You could use the context-key to change the value of this inside your callback:

您可以使用context-key來更改回調內的值:

function doDialogAjax(link: Link. modal: Modal) {
    $.ajax( link.Url,
    {
        cache: false,
        dataType: 'html',
        context: {
           link: link,
           modal: modal
        }
    })
        .done(onDialogDone)
        .fail(onDialogFail);
}

function onDialogDone(data: any, textStatus: string, jqXHR: JQueryXHR) {
    var x = data;
    // this refers to the context-object, with keys [link, modal]
    console.log(this.link);
    console.log(this.modal)
}

#2


-1  

.done(onDialogDone({link : link, modal : modal}, 'success', data))

Something like this?

像這樣的東西?


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/11/12/72a36a025638c81c95bd1c7a457ee5fe.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com