1
私はビューモデルのmodel.status
プロパティに応じて必要なdatetimepickerを持っています。どうすればこれを受け取ることができますか?ここでknockout.jsでdatetimepickerの 'required'バインディングを動的に変更できますか?
<div class='input-group date' data-bind="dateTimePicker: model.dateClaimClosed, dateTimePickerOptions: { required: lookUpViewValue(model.status) == 'Closed' }">
<input type='text' class="form-control submittable" id="dateClaimClosed" name="dateClaimClosed" placeholder="Select date..." />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</div>
は私bindingHandlerです:
ko.bindingHandlers.dateTimePicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
var options = {};
options.format = "DD/MM/YYYY";
$(element).datetimepicker(options);
ko.utils.registerEventHandler(element, "dp.change", function (event) {
var value = valueAccessor();
if (ko.isObservable(value)) {
if (event.date != null && !(event.date instanceof Date)) {
value(event.date != false ? event.date.toDate() : "");
} else {
value(event.date);
}
}
});
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
var picker = $(element).data("DateTimePicker");
if (picker) {
picker.destroy();
}
});
},
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var options = allBindings().dateTimePickerOptions || {};
var required = options.required || false;
valueAccessor().extend({ required: required });
ko.bindingHandlers.value.update(element, valueAccessor);
var picker = $(element).data("DateTimePicker");
if (picker) {
var koDate = ko.utils.unwrapObservable(valueAccessor());
if (koDate === undefined)
koDate = null;
else
koDate = (typeof (koDate) !== 'object') ? moment(koDate) : koDate;
picker.date(koDate);
}
}
};