I am doing a code challenge using Knockout.JS. It's pretty neat so far.
I have a couple simple questions on best practices, if someone could help me out. I'm trying to imagine what using this library will be like at a large scale but it's difficult.
When declaring the viewModel, is it usually better to only declare observables in the model when it's declared and tack on utility functions later? Or should functions integral to the viewModel be declared at the same time?
So this:
var viewModel = {
newItemName: ko.observable(),
newItemPrice: ko.observable(0),
newItemQuantity: ko.observable(1)
};
viewModel.incrementUp = function() {
var value = this.quantity() + 1;
this.quantity(value);
};
// more functions here
ko.applyBindings(viewModel);
versus. this:
var viewModel = {
newItemName: ko.observable(),
newItemPrice: ko.observable(0),
newItemQuantity: ko.observable(1),
addNewItem: function () {
var newItem = {
name: capitalizeFirst(this.newItemName()),
price: this.newItemPrice(),
quantity: ko.observable(this.newItemQuantity())
};
}
};
viewModel.something = ko.computed(function() {
return 'do stuff'
});
ko.applyBindings(viewModel);
Or do people normally just add on computed values after? I don't know if people would appreciate monolithic viewModel literals or if there's a standard to how this should be approached. I am used to Angular having 'the Angular way' to do everything, haha.
Sorry for being so pedantic. As I said, it's a code challenge and I want to impress...plus I am finding very difficult to find examples of Knockout apps.
Thanks!