{model : {"firstName":"David","lastName":"Bawa","state":"AL"}}
{"allStates":[{"id":1,"value":"AL","text":"ALABAMA"},{"id":2,"value":"AK","text":"ALASKA"}}
<select name="state" data-bind="options: allStates,optionsValue : 'value', optionsText: 'text' ,value: model.state,optionsCaption: 'Choose...'" id="ddlState"></select>
var registrationModel = {
staticData: function () {
var self = this;
self.allStates = ko.observableArray();
self.hearUsAll = ko.observableArray();
registrationService.getAllStates().done(function (result) {
if (result != null) {
$.each(result.List, function (i, v) {
self.allStates.push(v);
});
}
});
}
}
我的注册服务是
var registrationService = {
getAllStates: function () {
return service.staticService.get('GetAllStates');
}}
我的静态服务是
var service={
staticService: {
get: function (method) {
var results = store.fetch(method);
if (results) {
var dfd = new $.Deferred();
dfd.resolve(results);
return dfd.promise();
}
return $.ajax({
type: 'GET',
url: "http://xxxx/Service1.svc/" + method,
dataType: "json",
success: function (result) { if (result) { store.save(method,result) } }
}).promise();
}
状态从服务器通过ajax填充,如果我等待获取所有状态然后应用绑定,它可以正常工作。
正如您所看到的,我在模型中获得了预先选择的状态值。但是,当我不等待状态被填充并调用应用绑定时,它将无法工作。它将我的状态变量更改为null。我从映射插件中获取了我的视图模型。
请帮忙。