我正在尝试将snockoutjs与tempusdominus datetimepicker一起使用,并且自定义绑定有问题。该绑定将在初始化期间起作用,但是通过datepicker ui进行的更改或未更改将导致自定义绑定处理更新事件。请忽略缺少的图标,我认为这不是导致问题的原因。
ko.bindingHandlers.datepicker = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
//initialize datepicker with some optional options
var options = allBindings().datepickerOptions || {
format: 'MM/DD/YYYY HH:mm',
defaultDate: valueAccessor()()
};
$(element).datetimepicker(options);
//when a user changes the date, update the view model
ko.utils.registerEventHandler(element, "change.datetimepicker", function(event) {
var value = valueAccessor();
if (ko.isObservable(value)) {
value(event.date);
}
console.log("change.datetimepicker"); //, event.date.format());
});
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
var val = ko.utils.unwrapObservable(valueAccessor());
if ($(element).datetimepicker) {
$(element).datetimepicker("date", val);
}
console.log("update called"); //, $(element).datetimepicker("date").format());
}
};
var viewModel = {};
function QuoteViewModel(data) {
var self = this;
self.dt2 = ko.observable(moment());
};
viewModel = new QuoteViewModel();
ko.applyBindings(viewModel);
/*
$("#datetimepicker1").on("change.datetimepicker", function (e) {
console.log("on change", e.date.format());
});
*/
<link href="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/d004434a5ff76e7b97c8b07c01f34ca69e635d97/build/css/bootstrap-datetimepicker.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
<script src="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/d004434a5ff76e7b97c8b07c01f34ca69e635d97/src/js/bootstrap-datetimepicker.js"></script>
<div class="col-sm-6">
<div class="form-group">
<div class="input-group date" id="datetimepicker1" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker1" data-bind="datepicker: dt2" />
<div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
</div>
<pre data-bind="text: ko.toJSON($data, null, 2)"></pre> SCRIPT
https://jsfiddle.net/mh_surge/7yrze0Lv/6/
如果我取消注释添加非ko change事件处理程序的底部的代码,它将始终按预期触发。这使我相信ko.utils.registerEventHandler可能存在问题,但是该函数的源代码似乎还不错。
为什么更新和更改事件在初始化期间仅被触发一次,我该如何解决?
答案 0 :(得分:2)
所触发的更改事件不包含event.date
属性。您注释掉jQuery版本的原因是,它针对的是另一个元素。
在剔除代码中,您正在使用传递到element
和init
的{{1}}参数。这是指具有update
数据绑定的元素。
在jQuery代码中,您要定位datepicker
,这是一个$("#datetimepicker1")
,具有由插件实现的自定义<div>
元素。
要解决此问题,您需要:
change
绑定放在ID为datepicker
的元素上(即datetimepicker1
元素上的 not )input
函数中使用$(element).on("change.datepicker", ...)
语法答案 1 :(得分:0)
下面的修改对我有用,既可以用于日历弹出窗口,也可以用于文本输入的手动编辑。
ko.bindingHandlers.dateTimePicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
var options = allBindingsAccessor().dateTimePickerOptions || {};
var initialValue = ko.utils.unwrapObservable(valueAccessor());
if (initialValue) {
options.date = initialValue;
}
$(element).datetimepicker(options);
ko.utils.registerEventHandler(element, "change.datetimepicker", function (event) {
var value = valueAccessor();
if (ko.isObservable(value)) {
value(event.date || event.target.value || null);
}
});
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$(element).datetimepicker("destroy");
});
},
update: function (element, valueAccessor) {
var val = ko.utils.unwrapObservable(valueAccessor());
console.log(arguments, val);
if ($(element).datetimepicker) {
$(element).datetimepicker("date", val);
}
}
};