Reputation: 2276
I'm attempting to update a knockout view model, but each time I try to push to an observable array it gives me an error that AuditViewModel is undefined.
function (data, update) {
if (update == false) {
var AuditViewModel = {
auditEvents: ko.observableArray(data.requirements)
};
ko.applyBindings(AuditViewModel);
} else {
AuditViewModel.auditEvents.push(data.requirements);
}
}
On the page load this function always runs with update == false, and that originally populates the view model. When I later call with update ==true then it fails. Can someone point me in the right direction?
Upvotes: 0
Views: 46
Reputation: 715
Note that the line var AuditViewModel = { ... }
is only executed if update == false
. Because AuditViewModel is a local variable, if you later call the function when update == true
, AuditViewModel will be undefined.
You will need to store the view model elsewhere, ie: window.AuditViewModel = { ... }
.
Example:
function (data, update) {
if (update == false) {
window.AuditViewModel = {
auditEvents: ko.observableArray(data.requirements)
};
ko.applyBindings(window.AuditViewModel);
} else {
window.AuditViewModel.auditEvents.push(data.requirements);
}
}
Upvotes: 1