wait for ajax result to bind knockout model

前端 未结 1 1696
栀梦
栀梦 2021-01-24 04:51

I have getGeneral function that calls ajax GET. When ajax recieves data (json), it creates KO model from given json and returns created KO.

When Knockout mo

相关标签:
1条回答
  • 2021-01-24 05:23

    Try using the returned promise interface:

    function getGeneral(pid) {
        return $.ajax({
            url: "/api/general",
            contentType: "text/json",
            dataType: "json",
            type: "GET",
            data: {
                id: pid
            }
        });
    }
    
    getGeneral("@Model.Id").done(function (item) {
        var p = new GeneralModel();
        p = ko.mapping.fromJS(item);
        ko.applyBindings(p, document.getElementById("pv-portfolio-general-tab"));
    }).fail(function () {
        //handle error here
    });
    
    0 讨论(0)
提交回复
热议问题