[英]How do I access descendants from a different branch in the same viewModel?
我目前正在研究网站水疗中心应用程序,并尝试使用淘汰表.js。 它在带有dbdatacontroller api的mvc平台上,并使用upshot来存储数据和javascript视图模型。 我有一个复杂的视图模型,并且遇到了困难,主要是因为它是淘汰赛的新手。 我最大的问题似乎在于访问可观察对象。 数据库的组织方式如下:
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
似乎总是未定义。 就像我说的,我是淘汰赛的新手,所以我可能只是想念一些东西。 还是我应该使用多个视图模型或类似的东西? (如果是这样,我该怎么做?)谢谢。
你的:
<ol data-bind="foreach: AdvanceSearch">
需要是:
<ol data-bind="foreach: AdvanceSearchs">
注意AdvanceSearch到AdvanceSearchs的复数形式
编辑:
另外, UserValues
是一个可观察的数组,因此您无法访问UserValues().FieldValueText
。
请注意,请确定您的预期输出是什么,但这将显示每个UserValue
的输入:
<!-- ko foreach: UserValues -->
<input type="text" class="multilinetext" data-bind="attr: { id: 'value_'+UserProfileID()}, value: FieldValueText" />
<!-- /ko -->
注意:我已将输入的ID更改为其他名称以避免ID重复-假设UserProfileID
对于每个UserValue
都是唯一的
进一步,
请允许我参考Knockouts options-binding的文档。 select
元素的数据绑定选项为:
options:
-这是将select
元素绑定到的选项列表
optionsText:
对于列表中的每个项目,显示在select
元素中的属性
optionsCaption:
select
元素的第一个/“虚拟”项目的文本
value:
这应该是可观察的,并且绑定到select
元素的当前选择
value
绑定是Knockout的一个非常方便的功能。 由于您可以访问整个选定的对象,因此无需担心ID(因此也不必担心optionsValue
。
因此,您的select
绑定如下所示:
<select data-bind="options: AnswerType().Answers,
optionsText: 'AnswerText',
@* optionsValue: 'AnswerEnum', *@
optionsCaption: 'Not Specified',
value: selectedAnswer"></select>
假设您已向视图模型添加了一个可观察的selectedAnswer
。
根据您的评论,我想我现在知道您要执行的操作。 我将其抽象化,以便您的ViewModel和Objects非常复杂并且不加解释就不容易理解,因此易于理解。
如果您有对象类型,则使用构造函数的Book
和Author
:
function Book(data) {
this.ISBN = ko.observable( data.ISBN );
this.Title = ko.observable( data.Title );
this.AuthorID = ko.observable( data.AuthorID );
/* other properties */
}
function Author(data) {
this.AuthorID = ko.observable( data.AuthorID );
this.Name = ko.observable( data.Name );
/* other properties */
}
要从中选择一本书select
框,并有一个input
绑定到作者名称显示,你需要的视图模型:
var vm = {};
vm.books = ko.observableArray( /* initial data */ );
vm.authors = ko.observableArray( /* initial data */ );
vm.selectedBook = ko.observable(null);
vm.selectedBooksAuthor = ko.computed( function() {
var authors = vm.authors(),
selectedBook = vm.selectedBook();
if( !selectedBook ) return null; // cannot get .AuthorID() if selectedBook == null
var id = selectedBook.AuthorID();
for( var a in authors ) {
if( authors[a].AuthorID() == id )
return authors[a];
}
})
而您的HTML:
<select data-bind="options: books,
optionsText: 'Title',
optionsCaption: 'Select a book...',
value: selectedBook"></select>
<!-- ko with: selectedBooksAuthor -->
<input type="text" data-bind="value: Name" />
<!-- /ko -->
请注意,这在处理大量数据时可能会非常低效-考虑使用更好的搜索算法(而不是此处使用的“线性”搜索算法)来computed observable
即selectedBooksAuthor
编辑:
请参阅此小提琴以进行有效的演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.