I have a Single Page Application that uses knockout for the data binding. The CAApproval.html view in my single page application has an observeablearray named AllCertificates in the viewmodel code. It populates fine on the page. When you navigate away from the view by clicking a link in the navigation.html part of the page and then return to CAApproval page, the values from the previouse visit are still in the AllCertificates observableArray and therefore are displayed on the CAApproval view.
I need to clear the contents of the AllCertificates observablearray each time a user returns to the CAApproval page that uses that observablearray so that if a user leaves the page and comes back, the contents of the observablearray are null, and therefore no data is displayed on the screen. Here are the highlights of my viewmodel code-
define(['services/logger', 'durandal/system', 'durandal/plugins/router', 'services/CertificateDataService','controls/Lucas'],
function(logger, system, router, CertificateDataService) {
var allCertificates = ko.observableArray([]);
var activate = function () {
// go get local data, if we have it
return SelectAllCerts(),SelectMyCerts(), GetCertificateDetails(), GetDDABankNums();
};
var vm = {
activate: activate,
allCertificates: allCertificates,
SelectAllCerts: SelectAllCerts
});
return vm;
function SelectAllCerts() {
return CertificateDataService.getallCertificates(allCertificates);
}
});
How do I clear the contents of an observablearray each time the page a user comes to that page (NOT when navigating within the page itself, only clear the observablearray when the user comes from a seperate page)?
Also knockout observableArray
has interesting methods. Call removeAll
to clear all items.
Look at official site observable array manual.
self.mycertificates = ko.observableArray(['C1', 'C2']);
self.mycertificates.removeAll();