I’m looking for an example (jsfiddle if possible) of a custom binding for what we used to call a “compound component” years ago. I really don’t want someone to write my code for me. I just want to look over a few samples before I invest a lot of time in this, but I can't find any. Most custom binding examples are one way only. Just asking if you are aware of a sample that is similar …
Most of HTML/CSS is given to me by a design team so the layout isn’t always my choice. In this case there are dates, phone numbers and social security inputs that are all created using a common “theme”. That theme is to have three separate elements within a div. For example, the date has one for month, one for day and one for year. (I don’t believe we don’t need to complicate this with them being spinners). Validations and max/min input restrictions are expected.
I created an object as follows
var SplitDate = function (date) {
var self = this;
self.month = ko.observable();
self.day = ko.observable();
self.year = ko.observable();
var momentDate = moment(date);
if (momentDate.isValid()) {
self.month(momentDate.month() + 1);
self.day(momentDate.day());
self.year(momentDate.year());
}
}
In my viewmodel, I have tried both
self.dateOfBirth = new SplitDate(myDate);
and
self.dateOfBirth = ko.observable(new SplitDate(myDate));
but neither of those binds correctly using a standard value binding such as
data-binding = "value: dateOfBirth.day" or data-binding = "value: dateOfBirth().day"
So I'm presuming I need a custom binding. I'm not sure what's the best approach to take and if all of these need to be observables or not. We're using Knockout validations so I also expect I'll be adding a function for isValid() to the SplitDate.
So my question is, before I spend hours fumbling around with this, does anyone have a good example?