我目前正在开发一个网站 spa 应用程序并尝试使用 knockout.js。它在一个 mvc 平台上,一个 dbdatacontroller api 使用数据和 javascript 视图模型的结果。我有一个复杂的视图模型并且遇到了困难,主要是因为是新手。我最大的问题似乎是访问 observables。数据库是这样组织的:
function AdvanceSearch(data) {
var self = this;
self.AdvanceSearchID = ko.observable(data.AdvanceSearchID);
self.FieldTypeEnum = ko.observable(data.FieldTypeEnum);
self.AnswerType = ko.observable(data.AnswerType);
self.UserValues = ko.observableArray(ko.utils.arrayMap(data.UserValues, function (item) {
return new UserValue(item);
}));
upshot.addEntityProperties(self, "AdvanceSearch:#A.Lib.Repository");
};
function UserValue(data) {
var self = this;
self.UserProfileID = ko.observable(data.UserProfileID);
self.LoginID = ko.observable(data.LoginID);
self.AdvanceSearchID = ko.observable(data.AdvanceSearchID);
self.FieldValueCount = ko.observable(data.FieldValueCount);
self.FieldValueText = ko.observable(data.FieldValueText);
upshot.addEntityProperties(self, "UserValue:#A.Lib.Repository");
});
};
function AnswerType(data) {
var self = this;
self.AnswerTypeID = ko.observable(data.AnswerTypeID);
self.AnswerTypeText = ko.observable(data.AnswerTypeText);
self.Answers = ko.observableArray(ko.utils.arrayMap(data.Answers, function (item) {
return new Answer(item);
}));
self.AnswerSliders = ko.observableArray(ko.utils.arrayMap(data.AnswerSliders, function (item) {
return new AnswerSlider(item);
}));
upshot.addEntityProperties(self, "AnswerType:#A.Lib.Repository");
}
function Answer(data) {
var self = this;
self.AnswerTypeID = ko.observable(data.AnswerTypeID);
self.AnswerEnum = ko.observable(data.AnswerEnum);
self.AnswerText = ko.observable(data.AnswerText);
upshot.addEntityProperties(self, "Answer:#A.Lib.Repository");
}
function AnswerSlider(data) {
var self = this;
self.SliderID = ko.observable(data.SliderID);
self.AnswerTypeID = ko.observable(data.AnswerTypeID);
self.SliderType = ko.observable(data.SliderType);
self.Seed = ko.observable(data.Seed);
upshot.addEntityProperties(self, "AnswerSlider:#A.Lib.Repository");
}
我的视图模型就是这样的:
function ASViewModel() {
// Private
var self = this;
var dataSourceOptions = {
providerParameters: {
url: "/api/Dating",
operationName: "GetDatingProfile"
},
entityType: "AdvanceSearch:#A.Lib.Repository",
bufferChanges: false,
mapping: AdvanceSearch
};
// Public Properties
self.dataSource = new upshot.RemoteDataSource(dataSourceOptions)
.refresh();
self.AdvanceSearchs = self.dataSource.getEntities();
}
所以我的标记类似于
<ol data-bind="foreach: AdvanceSearch">
<!-- ko if: FieldTypeEnum()===5 -->
<select data-bind="options: AnswerType().Answers, optionsText: 'AnswerText', optionsValue: 'AnswerEnum', optionsCaption: 'Not Specified', value: UserValues().FieldValueText"></select>
<!-- /ko -->
<!-- ko if: FieldTypeEnum()===11 -->
<input type="text" class="multilinetext" data-bind="attr: { id: 'value_'+AdvanceSearchID()}, value: UserValues().FieldValueText" />
<!-- /ko -->
所以基本上,无论我做什么,我似乎都无法访问项目的值。我访问过一个分支的任何地方,即。AdvanceSearch().AnswerType().Answers
,试图得到$parents[1].UserValues[].FieldValueText
似乎总是不确定的。就像我说的,我是淘汰赛的新手,所以我可能只是错过了一些东西。还是我应该使用多个视图模型或类似的东西?(如果是这样,我该怎么做?)谢谢。