I am new to knockout, and I am trying to create a function that I can bind to in my view, so that different input boxes will be bound to different elements of the array.
Unfortunately, the code that calls the function in knockout seems to happen before the array is populated. I am sure there is some simple way that I can fix this, but I have been trying for a while and just can't figure it out
(function () {
var BankingViewModel = function () {
//data
var self = this;
self.safeFloatTotal = ko.observable(null);
self.floatRecommendedValue = ko.observable(null);
self.safeFloatDenominations = ko.observableArray();
//populate the array
var safeFloatCash = bankingApi.client.getSafeFloatCash();
safeFloatCash.done(function (d) {
self.safeFloatDenominations(d);
})
self.GetNoteByDenomination = ko.computed( function () {
// return 1234; //will bind OK
return self.safeFloatDenominations[1]; //length of the array is zero when this is called, so element is undefined
});
}
$(document).ready(function () {
var viewModel = new BankingViewModel();
ko.applyBindings(viewModel);
});
})();
and in the view
<div class="row">
<div class="col-xs-6">
<input type="text" data-bind="value: GetNoteByDenomination" />
<label>£50</label>
</div>
<div class="col-xs-6">
<input type="text" data-bind="value: safeFloatTotal" />
<label>50p</label>
</div>
</div>
I need to be able to call the array after it is initialised, which is where I am stuck at the moment