简体   繁体   English

$ .getJSON无法传递到knockout observableArray

[英]$.getJSON can't be passed into knockout observableArray

I'm trying to call a Api that returns Json. 我正打算召唤一个返回Json的Api。 I'm attempting to put this returned data into a knockout observable array. 我试图将这些返回的数据放入一个可淘汰的可观察数组中。 My view model looks like this: 我的视图模型如下所示:

var adminData = $.getJSON("/api/administrators");
//console.log(adminData);

var viewModel = {
    administrators: ko.observableArray(adminData)
};

ko.applyBindings(viewModel);

The request goes through and an object is being returned with the expected data in adminData, but when I try to add it to ko.observableArray I get this in the console: The argument passed when initializing an observable array must be an array. 请求通过并返回一个对象,其中包含adminData中的预期数据,但是当我尝试将其添加到ko.observableArray时,我在控制台中得到了这个:初始化可观察数组时传递的参数必须是数组。 I can't figure out how to get that data into an array for knockout. 我无法弄清楚如何将数据转换为数组以进行淘汰赛。

$.getJSON in asynchronous. 异步中的$.getJSON The result data is available in the callback only. 结果数据仅在回调中可用。 It is not available as the return value of $.getJSON The documentation never mentions a return value. 它不能作为$.getJSON的返回值。文档从不提及返回值。

$.getJSON("/api/administrators", null, function(adminData, status, xhr){
    var viewModel = {
        administrators: ko.observableArray(adminData)
    };  
    ko.applyBindings(viewModel); 
});

If you need to make separate AJAX calls, you should use jQuery.when See Wait until all jQuery Ajax requests are done? 如果你需要进行单独的AJAX调用,你应该使用jQuery.when看到等待直到所有jQuery Ajax请求完成?

$.when($.ajax("/api/administrators"), $.ajax("api/roles")).done(function(resp1, resp2){        
    ko.applyBindings({
        administrators: ko.observableArray(resp1[0]),
        roles: ko.observableArray(resp2[0]);
    }); 
});

Here are some other less than ideal solutions, but it shows you what happens under the hood. 以下是一些其他不太理想的解决方案,但它向您展示了幕后发生的事情。

If you don't mind that the requests wait for each other 如果您不介意请求彼此等待

$.getJSON("/api/administrators", null, function(adminData){
    $.getJSON("/api/administrators", null, function(apiRoles){
        ko.applyBindings({
            administrators: ko.observableArray(adminData),
            roles: ko.observableArray(apiRoles);
        }); 
    });
});

If you do care, it's more complicated since you need to track that the requests finished 如果你关心,它会更复杂,因为你需要跟踪请求已完成

var ajaxAdminData, ajaxApiRoles
$.getJSON("/api/administrators", null, function(adminData, status, xhr){
    var ajaxAdminData = adminData;
    // If the other call finished, apply the bindings
    if (ajaxApiRoles) {
        applyBindings();
    }
});

$.getJSON("/api/administrators", null, function(apiRoles, status, xhr){
    ajaxApiRoles = apiRoles;
    // If the other call finished, apply the bindings
    if (ajaxAdminData) {
        applyBindings();
    }
});

function applyBindings() {
    ko.applyBindings({
        administrators: ko.observableArray(ajaxAdminData),
        roles: ko.observableArray(ajaxApiRoles);
    }); 
}

Because getJSON() is asynchronous! 因为getJSON()是异步的! You can not treat it as a synchronous method. 您不能将其视为同步方法。 Look at what that console.log line is, it would show why it failed. 看看console.log行是什么,它会显示失败的原因。

Use the callback 使用回调

$.getJSON("/api/administrators", function(adminData) {

    var viewModel = {
        administrators: ko.observableArray(adminData)
    };

    ko.applyBindings(viewModel);
}

在$ .getJSON回调中,只需更新您的observable数组,然后在该数组上调用valueHasMutated(),如:administrators.valueHasMutated()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM