Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript在windows8中填充列表_Javascript_Windows 8 - Fatal编程技术网

如何使用javascript在windows8中填充列表

如何使用javascript在windows8中填充列表,javascript,windows-8,Javascript,Windows 8,我正在尝试构建windows8应用程序,我使用SplitApp作为基本应用程序。只是尝试从AJAX添加数据,但失败了 在data.js文件中,我有: (function () { var list = new WinJS.Binding.List(); $.each(data(), function (key, item) { list.push(item); }); } })(); 在app.js文件中,我有(这可以工作并填充应用程序中的列表)

我正在尝试构建windows8应用程序,我使用SplitApp作为基本应用程序。只是尝试从AJAX添加数据,但失败了

在data.js文件中,我有:

(function () {

    var list = new WinJS.Binding.List();

    $.each(data(), function (key, item) {
        list.push(item);
    }); 

}
})();
在app.js文件中,我有(这可以工作并填充应用程序中的列表)

但是,当我想使用AJAX获取数据并在填充testMeeting时返回它时,它会崩溃

在app.js文件中,我有(不起作用),但我需要让它起作用

function data() {

   var testGroupMeeting = [];
   var testMeeting = [];

$.ajax({
    url: "/json/json.php",
    dataType: 'json',
    contentType: 'text/json',
    type: 'GET',
    success: function (data) {

           //Data here is correct and mapped to the arrays, its the same as in the abow example, i have the same data in the arrays as in the above example



        }
        return testMeeting;
    }

});


}
但问题似乎是AJAX不应该返回任何内容。我无法回调data.js,因为正如您所看到的,该函数是匿名的


您将如何做到这一点?

这不能以这种方式工作,因为$.ajax函数是异步的:它执行ajax调用,然后使用适当的数据调用“success”函数

您需要重写$.each(data()…以便调用data()并期望它返回testMeeting,而不是调用data并期望它使用testMeeting对象调用回调

比如:

(function () {

    var list = new WinJS.Binding.List();

    getData(function (theMetting) {


        $.each(theMeeting, function (key, item) {
          list.push(item);
        }); 

 }
})();


// callback is a function that will be called with 
// something that is built from the server, after the ajax
// request is done
function getData(callback) {


 $.ajax({
    // ... everything you did ... 
    success: function (data) {

       // ... use the data to build the meeting object
       // and pass it to the callback
       callback(meeting)


    }
    return testMeeting;
}

});

}
同步代码(返回函数)和异步调用(做一些工作,然后调用回调并得到结果)之间有一个根本的区别。$ajax是典型的调用异步函数

理论上,您可以将“async”标志传递给ajax,以便在ajax调用完成之前,$.ajax函数不会返回,但您可能不想这样做,因为它会阻塞您的UI


希望这能有所帮助。

问题在于getData(函数(主题设置){$.each(主题设置,函数(键,项){list.push(项);})}默认情况下是microsoft的匿名函数,我无法按照您的建议从我的app.js访问它,或者?不,getData只是您可以编写的函数,而不是“data”函数。我假设所有代码都是您的-如果您无法从data.js更改函数,并且您可以修改应用程序的唯一方法是定义“data”函数,那么您必须创建一个同步(而不是异步)函数调用您的服务器-您可以通过向$.ajax函数传递一个标志来完成此操作。然后,$.ajax函数将等待“成功”回调完成…现在,要明确的是,我并不真正理解您所说的“在ms的默认匿名函数中”是什么意思。我所能告诉您的是$.each(data(),…)仅当data()返回数组时,版本才起作用,而您将无法编写从异步调用返回数据到服务器的函数。要么将其设置为同步调用,要么重写循环以适应它。希望这能有所帮助。
(function () {

    var list = new WinJS.Binding.List();

    getData(function (theMetting) {


        $.each(theMeeting, function (key, item) {
          list.push(item);
        }); 

 }
})();


// callback is a function that will be called with 
// something that is built from the server, after the ajax
// request is done
function getData(callback) {


 $.ajax({
    // ... everything you did ... 
    success: function (data) {

       // ... use the data to build the meeting object
       // and pass it to the callback
       callback(meeting)


    }
    return testMeeting;
}

});

}