如何清除以前访问视图时填充的observableArray的内容 [英] How to Clear Contents of an observableArray That was Populated from Previous Visits to a View

查看:83
本文介绍了如何清除以前访问视图时填充的observableArray的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单页应用程序,该应用程序使用敲除进行数据绑定.我的单页应用程序中的CAApproval.html视图在视图模型代码中有一个名为AllCertificates的observablearray.它可以在页面上很好地填充.当您通过单击页面的navigation.html部分中的链接离开视图,然后返回到CAApproval页面时,先前访问的值仍在AllCertificates observableArray中,因此将显示在CAApproval视图中.

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.

每当用户返回使用该ObservableArray的CAApproval页面时,我需要清除AllCertificates observablearray的内容,以便如果用户离开该页面并返回,则observablearray的内容为null,因此没有数据屏幕上显示.这是我的视图模型代码的重点-

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);
        }
    });

如何在用户每次访问该页面时清除observablearray的内容(在页面本身中导航时不是,仅当用户来自单独页面时才清除observablearray)?

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)?

推荐答案

只需在您的激活函数(每次加载视图模型时都会调用它)中将其设置为等于空(allCertificates([]))-

Just set it equal to nothing (allCertificates([])) in your activate function, which is called each time your view model loads -

function(logger, system, router, CertificateDataService) {
    var allCertificates = ko.observableArray();

var activate = function () {
    allCertificates([]);
    // go get local data, if we have it
    return SelectAllCerts(),SelectMyCerts(), GetCertificateDetails(), GetDDABankNums();
};

var vm = {
    activate: activate,
    allCertificates: allCertificates,
    SelectAllCerts: SelectAllCerts
});

这篇关于如何清除以前访问视图时填充的observableArray的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆