I develop a project with knockout/breeze.
I would like to know if it is possible to force knockout to mark an observable as changed (even if focus is still in the field). My goad is to notify user that whenever he begin to change the date he has the ability to save it (with a button showed immediately). For example, I have an input field with dates. User begin to edit the date in this field. The observable should only interpret the new encoded date when user leave focus of the field. But I would like to show my Save button
as soon as he begin to type something in the input field. I hope I'm clear.
Here is my bindingHandlers for taking care of editing dates in my input:
ko.bindingHandlers.dateRW = {
//dateRW --> the 'read-write' version used both for displaying & updating dates
init: function (element, valueAccessor, allBindingsAccessor) {
var observable = valueAccessor();
var value = ko.utils.unwrapObservable(valueAccessor);
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var myDate = moment($(element).val(), "DD/MM/YYYY");
observable(myDate.toDate());
});
//ko.utils.registerEventHandler(element, "keyup", function () {
// As soon as user begin to type something, I would like to show my save button
//});
},
update: function (element, valueAccessor, allBindingsAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
var date = (typeof value !== 'undefined') ? moment(value) : null;
var dateFormatted = (date != null) ? date.format('DD/MM/YYYY') : '';
$(element).val(dateFormatted);
}
};
And my view:
<input type="text" data-bind="dateRW: myDate" />
Unfortunately valueUpdate: 'afterkeydown'
does not work because I'm using a custom bindingHandlers.
Hope someone could point me in the right direction.
Thanks.