将一些参数传递给 WinJs xhr 的回调函数 [英] passing some argument to the callback function of WinJs xhr

查看:23
本文介绍了将一些参数传递给 WinJs xhr 的回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 8 中,我在循环中使用 WinJs.xhr 来下载一些内容,当它在完成回调"之后到达时,我想传递一个参数来检索调用 xhr 的元素.

In Windows 8, I am using WinJs.xhr in a loop to download some content, and as it arrives afterward to the "done callback", I would like to pass an argument to retrieve the element who call xhr.

    for (var k = 0 ; k < 9; k++) {
                        var title = dataArray[k].name;
                        if (title != null)
                            url = monUrl+ title;

                        WinJS.xhr({ url: url, responseType: "responseXML" })
   .done(function complete(result) {

    //I would like to retrieve the right title here for example
       var dataArray = new Array();
       var xml = result.responseXML;
 }
}

感谢您的帮助

推荐答案

你需要在一个闭包中捕获它,这对于普通的 for 循环/迭代来说是一种痛苦:

You'll need to capture it in a closure, which with vanilla for loops/iteration is a pain:

for (var k = 0 ; k < 9; k++) {
    (function(item) {
        var url;
        var title = item.name;
        if (title != null)
            url = monUrl+ title;

        WinJS.xhr({ url: url, responseType: "responseXML" }).done(function complete(result) {
            // I would like to retrieve the right title here for example
            var dataArray = new Array();
            var xml = result.responseXML;
            /* use your title property here */
        });
    })(dataArray[k]);
}

这篇关于将一些参数传递给 WinJs xhr 的回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆