本文介绍了如何使用javascript在windows8中填充列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im trying to build an windows8 app, i use the SplitApp as basic. Just trying to add data from AJAX but it fails.

在文件 data.js 中,我有:

In the file data.js i have:

(function () {

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

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

}
})();

在文件 app.js 中我有 (这有效并填充应用程序中的列表)

In the file app.js i have (This works and populates the list in the app)

function data() {

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

   testGroupMeeting.push(new Group({ id: "1", title: "Group1" }));

   testMeeting.push(new Meeting({ group: testGroupMeeting[0], title: "Item Title: 1"       }));

   return testMeeting;


}

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

But when i want to use AJAX to get data and return testMeeting when it is populated it crashes.

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

In the file app.js i have (Doesnt work) but i need to get this to work

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 进行回调,因为如您所见,该函数是匿名的.

But the problem seems to be that AJAX is not supposed to be returning anything. And i cant do a callback to data.js because that function is anonymous as you can see.

你会怎么做?

推荐答案

这样不行,因为 $.ajax 函数是异步的:它先调用 ajax,然后再调用success"函数用适当的数据.

This can not work this way, because the $.ajax function is asynchronous : it does the ajax call, and then, later, calls the "success" function with the appropriate data.

您将重写 $.each(data() ...,这样您就不必调用 data() 并期望它返回 testMeeting ,而是调用 data 并期望它使用 testMetting 对象调用回调.

You'll have rewrite the $.each(data() ... so that instead of calling data() and expecting it to return testMeeting , you call data and expect it to call a callback with the testMetting object.

类似:

(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 是典型的异步函数.

There is a fundamental difference between synchronous code (that returns functions) and asynchonous calls (that does some work, and later calls callback with the result). $.ajax is the typicall asynchronous function.

可以理论上将async"标志传递给ajax,这样$.ajax 函数就不会在Ajax 调用完成之前返回,但您可能不想这样做,因为它会阻止你的用户界面.

You could in theory pass the "async" flag to ajax, so that the $.ajax function does not return before the Ajax call is done, but you probably do not want to do that since it would block you UI.

希望这会有所帮助.

这篇关于如何使用javascript在windows8中填充列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:47