I am having a problem with binding a date to the object on my viewmodel. I have a date that I am getting from the server.
var viewModel = {
profile : ko.mapping.fromJS(initialData),
I am binding the property to a text box.
<input data-bind="datepicker: profile.Birthdate()" />
I am using the custom binding that I found on this here: http://jsfiddle.net/rniemeyer/NAgNV/
ko.bindingHandlers.datepicker = {
init: function(element, valueAccessor, allBindingsAccessor) {
//initialize datepicker with some optional options
var options = allBindingsAccessor().datepickerOptions || {};
$(element).datepicker(options);
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var observable = valueAccessor();
ko.observable($(element).datepicker("getDate"));
$(element).blur();
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).datepicker("destroy");
});
},
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor()),
current = $(element).datepicker("getDate");
if(value != null)
{
if(value.toString()[0] = "/")
value = new Date(parseInt(value.toString().substr(6)));
}
if (value - current !== 0) {
$(element).datepicker("setDate", value);
}
}
};
I added the if(value.toString()[0] = "/") for formatting of the date to display in the textbox.
This seems to work well except when I try to save the object add send it back the the server. I added an alert to verify and before I send the object to the server there is no change in the date.
save : function(){
alert(this.profile.Birthdate);
Any ideas on what I am doing wrong?
Thank you for your time.