我正在使用MVC框架和淘汰赛js组合。我对淘汰JS有点陌生。我需要通过嵌套的剔除模板中的API调用动态绑定数据。我没有找到有关如何执行此操作的任何方法。
我的嵌套模板是:
enter code here
<div data-bind="template: {name: 'ListTemplate', foreach: Data}">
</div>
<script type="text/html" id="ListTemplate">
<h3>
Contributions (${ Count })
</h3>
<img src=" ${ Image } " />
<p>
<span> ${ Name } </span>
<div data-bind="template: {name: 'goalsTemplate', foreach: goals}"></div>
</p>
</script>
<script type="text/html" id="goalsTemplate">
Goal:
<a href="#"> ${ Goals } </a> Ends on
<code> ${ Date } </code>
</script>
而我的viewModel是:
var viewModel = {(
Data: ko.observableArray([]),
goals: ko.observableArray([])
});
function userData(Count, Image, Name) {
return {
Count: ko.observable(Count),
Image: ko.observable(Image),
Name: ko.observable(Name)
};
}
function goalDetail(Goals, Date) {
return {
Goals: ko.observable(Goals),
Date: ko.observable(Date)
};
}
$(document).ready(function() {
$.ajax({
type: "GET",
url: siteBaseUrl + 'api/Detail',
dataType: 'json',
success: function (data) {
$(data).each(function (index, type) {
viewModel.Data.push(new userData(..,..,..));
});
},
error: function (xhr, ajaxOptions, thrownError) {
alert('error status code:' + xhr.status);
alert('error message:' + thrownError);
alert('error details:' + xhr.responseText);
}
});
});
我应该如何通过数据数组中的function(goalDetail)将数据绑定到目标数组中?
最佳答案
根据我的理解,“目标和数据”是main viewModel的一部分,您想在Foreach绑定中使用Parent Viewmodel,在这种情况下,您需要的是$ parent,如下所示
<div data-bind="template: {name: 'goalsTemplate', foreach: $parent.goals}">