I am somewhat new to knockout.js and I'm having trouble wrapping my head around where to put certain operations on observable arrays. Here is what my code looks like:
var Building = function() {
var self = this;
self.nLocation = ko.observable();
self.nBuilding = ko.observable();
...
self.increaseLocNum = function() {
self.nLocation(self.nLocation + 1);
};
}
var Quote = function() {
var self = this;
var nQuoteID = ko.observable();
...
self.buildings = ko.observableArray([]);
self.addBuilding = function(){
...
// Build new building with next loc/building number
buildings.push();
}
...
}
ko.applyBindings(new Quote());
So essentially I have a quote that can have multiple buildings on it. Each building is bound to a different tab on a tab control. On these tabs is a 'Location' field that has +/- buttons that increase/decrease the location number.
I need to use 'enable' binding to set the enable of the +/- buttons (for example, if a building is the only building at the highest location number. Here are a few examples of simple rules:
- Location number can't be decreased if the building is at location 1
- Location number can't be increased if the building is the only building on the highest location
- Location number can't be increased if there is only one building
The logic is pretty straight forward but I'm lost on where this logic should go to follow best knockout practices.