目前我在activate方法中有一个方法从rest服务获取数据,问题是视图正在等待promise完成,如何加载视图并获取数据而不阻塞activate方法
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;
});
答案 0 :(得分:1)
只需在datacontext.PersonData()调用前面添加单词return,它应立即显示视图...唯一不行的方法是如果你的datacontext.PersonData()没有返回任何内容数据已从服务器返回。
var person = ko.observable();
var activate = function() {
return datacontext.PersonData(person);
}
如果您的PersonData正在阻止您的通话,那么请将您的人或其他任何内容传入您的datacontext并将其设置为ajax通话的成功。
var PersonData(person) = function () {
return $.ajax(function () { // do some ajax call
success: function (data) {
person(data);
});
}
答案 1 :(得分: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:false,}); },
ASYNC:FALSE
叹气:)
感谢橡胶躲避家伙:)