knockoutjs绑定到$ parent问题

时间:2014-01-30 09:50:35

标签: 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(''),
        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:无法调用未定义的方法'假日'。有什么问题?请帮忙。

<!-- 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 -->

1 个答案:

答案 0 :(得分:1)

您无法使用$parent.$parent,而应使用$parents[1]

请参阅docs