Javascript knockoutjs绑定到$parent问题
我正在将视图绑定到以下viewmodel。不过,我只分享其中的相关部分Javascript knockoutjs绑定到$parent问题,javascript,knockout.js,Javascript,Knockout.js,我正在将视图绑定到以下viewmodel。不过,我只分享其中的相关部分 self.store_data = { id: ko.observable(-1), business_name: ko.observable(''), company_number: ko.observable(''), trading_number: ko.observable(''), address: ko.observable(''),
self.store_data = {
id: ko.observable(-1),
business_name: ko.observable(''),
company_number: ko.observable(''),
trading_number: ko.observable(''),
address: ko.observable(''),
postcode: ko.observable(''),
phone_number: ko.observable(''),
opening_hours: ko.observable(''),
closing_hours: ko.observable(''),
is_bank_holiday: ko.observable(false),
business_photo: ko.observable(null),
business_photo_type: ko.observable('jpeg'),
lat: ko.observable(''),
lng: ko.observable(''),
is_premium: ko.observable(false),
is_closed: ko.observable(0),
catalog : ko.observable(), //selected catalog
timings_calendar : {
'mon': {
holiday: ko.observable(false),
timings: ko.observableArray([
new self.Timings('9:00:00', '01:00:00'),
new self.Timings('02:00:00', '06:00:00')
])
},
'tue': {
holiday: ko.observable(false),
timings: ko.observableArray([
new self.Timings('9:00:00', '01:00:00'),
new self.Timings('02:00:00', '06:00:00')
])
},...
下面是视图,其中data bind=“checked:$parent.$parent.holiday()==false”
未绑定此错误“TypeError:无法调用未定义的方法”holiday“。问题是什么?请帮忙
<!-- ko foreach: store_data.timings_calendar.mon.timings -->
<div class="ui-block-a" style="width: 30%;">
<!-- ko if: $index() === 0 -->
<input type="checkbox" data-bind="checked: $parent.$parent.holiday() == false" id="monday-timings-checkbox" data-mini="true" /> <label for="monday-timings-checkbox">Mon</label>
<!-- /ko -->
</div>
<div class="ui-block-b" style="width: 30%;">
<select data-mini="true" data-bind="options: $root.timeModel.timings, optionsText: 'text', optionsValue: 'value', value: $data.opening_hours "></select>
</div>
<div class="ui-block-c" style="width: 30%;">
<select data-mini="true" data-bind="options: $root.timeModel.timings, optionsText: 'text', optionsValue: 'value', value: $data.closing_hours "></select>
</div>
<div class="ui-block-c" style="width: 10%;">
<!-- ko if: $data.opening_hours() != '' && $data.closing_hours() != '' && $index() != $parent.length -1 -->
<button data-mini="true" data-icon="delete" data-iconpos="notext" data-theme="a"></button>
<!-- /ko -->
<!-- ko ifnot: $data.opening_hours() != '' && $data.closing_hours() != '' && $index() != $parent.length -1 -->
<button data-mini="true" data-icon="plus" data-iconpos="notext" data-theme="b"></button>
<!-- /ko -->
</div>
<!-- /ko -->
周一
您不能使用$parent.$parent
您应该使用$parents[1]
请参阅。粘贴代码并格式化比上传代码图像更好。人们更容易用这种方式编辑和发布答案。最好还是创建一个JSFIDLE来演示这个问题。为了方便起见添加了text。实际上,它可能是$parents[1]的副本。但是你链接的文档中已经说明了这一点:)$parents[1]看起来不错,但它给出了相同的错误。无论如何,我可以用
data bind=“checked:$root.store\u data.timings\u calendar.mon.holiday()==false”来完成这项工作。
只是一个旁注,与viewmodels结构的这种耦合级别是不好的。如果您有某种事件总线,可以在假日更改时发布,则会更好。因此,使用$root比$parent更脆弱,因为如果您选择在顶部插入新的viewmodel,它的绝对值将被破坏