I'm currently designing a in-browser calculator, and the functionality I want is to create an input box, and bind it to a object in the view-model.
This object will have a property called value, which will be what the input box displays, but I also want it to have min and max limits which which change the background colour of the box to red if they are exceeded.
I got the basic input binding to work, but I'm having trouble creating my own custom binding wrapper for the input binding that also changes the background colour.
My HTML:
<td><input data-bind="calcVar: resistance" type="text" size="16" /></td>
My Javascript:
The "class" to hold all the data
var calcVar = function(value, lowerBound, upperBound) {
this.value = ko.observable(value);
this.lowerBound = ko.observable(lowerBound);
this.upperBound = ko.observable(upperBound);
};
Creating a variable in the view-model:
this.fSwAct = ko.observable(new calcVar(200, 100, 100, 0, 1000));
Start-up function
// Start-up function
j(document).ready(
function StartUp()
{
// Create custom binding
ko.bindingHandlers.calcVar = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
ko.bindingHandlers.value.init(element, valueAccessor()().value, allBindings, viewModel, bindingContext);
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
// Call value binding (child binding)
ko.bindingHandlers.value.update(element, valueAccessor()().value, allBindings, viewModel, bindingContext);
}
};
// Activates knockout.js
var app = new AppViewModel();
ko.applyBindings(app);
}
);
Although the custom binding function is being called, the input bindings don't seem to work, and other calculated fields don't update when the value is changed. I feel it's something to do with the way I am creating the calcVar "class" or passing it it in to the input binding.