等待 ajax 结果绑定淘汰模型 [英] wait for ajax result to bind knockout model

查看:20
本文介绍了等待 ajax 结果绑定淘汰模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有调用 ajax GET 的 getGeneral 函数.当ajax收到数据(json)时,它从给定的json创建KO模型并返回创建的KO.

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

当创建 Knockout 模型并分配值时,应调用 Knockout applybindings.这是我的代码:

When Knockout model is created and values are assigned, knockout applybindings should be called. Here is my code:

定义GeneralModel和一些相关函数(在GeneralModel.js"中):

Defines GeneralModel and some related functions (inside "GeneralModel.js"):

var GeneralModel = function() {

   //for now it is empty as data ar binded automatically from json
   // CountryName is one of the properties that is returned with json
} 

function getGeneral(pid) {
    $.ajax({
        url: "/api/general",
        contentType: "text/json",
        dataType: "json",
        type: "GET",
        data: { id: pid},
        success: function (item) {
            var p = new GeneralModel();
            p = ko.mapping.fromJS(item);
            return p;
        },
        error: function (data) {

        }
    });    
}

这是从另一个文件 (GeneralTabl.html) 调用的,它应该调用 get 函数和 applyBindings 来更新 UI:

This is called from another file (GeneralTabl.html), it should call get function and applyBindings to update UI:

var PortfolioGeneral = getGeneral("@Model.Id");
ko.applyBindings(PortfolioGeneral, document.getElementById("pv-portfolio-general-tab"));

但是,在这种情况下,我收到错误消息(未定义国家/地区名称).这是因为 applyBindings 在 ajax 返回数据之前发生,所以我正在对具有未定义属性的空模型执行 applyBindings.

However, in this scenario I am getting error (CountryName is not defined). This is because applyBindings happens before ajax returns data, so I am doing applyBindings to empty model with undefined properties.

从 Json 到 Model 的映射发生在这里并赋值:p = ko.mapping.fromJS(item);

Mapping from Json to Model happens here and is assignes values: p = ko.mapping.fromJS(item);

我也可以用所有字段填写GeneralModel,但没有必要(我猜):

I can also fill in GeneralModel with all fields, but it is not necessary (I guess):

  var GeneralModel = function() {    
      CountryName = ko.observable();
      ...
  } 

它仍然会给出错误CountryName is not defined".

It will still give an error "CountryName is not defined".

解决办法是什么?

1) 我能否以某种方式将 getGeneral 移动到 GeneralModel 中,以便获取数据成为 GeneralModel 初始化的一部分?

1) Can I somehow move getGeneral inside GeneralModel, so get data would be part of GeneralModel initialization?

2) 也许我应该以某种方式等待 ajax 结果"然后才applyBindings?

2) Maybe I should somehow do "wait for ajax results" and only then applyBindings?

我相信还有其他选择,只是我对 KO 和纯 JS 不太熟悉.

I believe there are other options, I am just not so familiar with KO and pure JS.

注意:我完全理解这是因为 Ajax 是 Async 调用,所以问题是考虑到我有两个单独的文件并且我需要从外部调用 getGeneral 并且它应该返回一些变量,因此问题是如何重构此代码.

Note: I fully understand that this is because Ajax is Async call, so the question is how to restructure this code taking into account that I have two seperate files and I need to call getGeneral from outside and it should return some variable.

推荐答案

尝试使用返回的promise接口:

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
});

这篇关于等待 ajax 结果绑定淘汰模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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