0

目前我在激活方法中有一个从休息服务获取数据的方法,问题是视图正在等待承诺完成,如何在不阻塞激活方法的情况下加载视图并获取数据

var activate = function() {
     datacontext.PersonData().then(function(result) {
            name(result.person.firstName + " " + result.person.lastName);
            if (result.person.gender != undefined) {
                sex("(" + result.person.gender.substr(0, 1) + ")");
            }
            dob(generalFunctions.convertTimeStamp(result.person.birthDate, false));
            inss(result.person.inss);
        }); 
}

return { activate: activate }

完整代码 -->

define(['durandal/app',
    'knockout',
    'core/config',
    'services/datacontext',
    'core/generalFunctions'],
function (app, ko, config, datacontext, generalFunctions) {
var name = ko.observable();
var sex = ko.observable();
var dob = ko.observable();
var inss = ko.observable();
var address = ko.observable();
var city = ko.observable();
var phone = ko.observable();
var personIsSelected = ko.observable(false);
var personIsNotSelected = ko.observable(false);
var activate = function (id) {
    config.isLoading(false);
    if (id == null) {
        if (config.selectedPatientId().length == 0) {
            personIsNotSelected(true);
            personIsSelected(false);
        } else {
            personIsNotSelected(false);
            personIsSelected(true);
        }
    } else {
        config.selectedPatientId(id);
        personIsSelected(true);
        personIsNotSelected(false);
    }
    config.pageTitle("Dashboard");
    setDashboardData();
};


var setDashboardData = function () {
    var id = config.selectedPatientId();
    //Fill basc patient data

    datacontext.patientBasicData(id).then(function(result) {
        name(result.person.firstName + " " + result.person.lastName);
        if (result.person.gender != undefined) {
            sex("(" + result.person.gender.substr(0, 1) + ")");
        }
        dob(generalFunctions.convertTimeStamp(result.person.birthDate, false));
        inss(result.person.inss);
    });

    //Fill adresss
    datacontext.patientAddress(id).then(function(result) {
        var firstAdress = result[0];
        address(firstAdress.street);
        city(firstAdress.zip + " " + firstAdress.municipality);
        phone(firstAdress.cellPhoneNumber);
    });


    //datacontext.medicationEntries(id).then(function (result) {
    //    console.log("medicationEntries");
    //    console.log(result);
    //});


    //datacontext.careElements(id).then(function (result) {
    //    console.log("CareElements");
    //    console.log(result);
    //});
};

var vm = {
    personIsSelected: personIsSelected,
    personIsNotSelected: personIsNotSelected,
    name: name,
    sex: sex,
    dob: dob,
    inss: inss,
    address: address,
    city: city,
    phone: phone,
    activate: activate,      

};

return vm;
});
4

2 回答 2

1

只需在您的 datacontext.PersonData() 调用前面添加单词 return ,这应该立即显示视图......唯一不起作用的方法是如果您的 datacontext.PersonData() 在数据有之前不返回任何内容已从服务器返回。

var person = ko.observable();

var activate = function() {
     return datacontext.PersonData(person); 
}

如果您的 PersonData 阻碍了您的通话,则将您的人或其他任何内容传递到您的数据上下文中,并将其设置为 ajax 调用的成功。

var PersonData(person) = function () {
    return $.ajax(function () { // do some ajax call
                    success: function (data) {
                        person(data);
                    });
}
于 2013-09-10T19:19:28.157 回答
0

Soo PW Kad,你让我想到了,我去查看 http 模块并看到以下内容:

get: function (url, query, type, header) { return $.ajax(url, { data: query, headers: { 'Authorization': header }, dataType: "json", crossDomain: true, type: type, async : 错误的, }); },

异步:假

叹息:)

感谢橡胶躲避家伙:)

于 2013-09-10T20:13:12.343 回答