KnockoutJS /引导 - 使用JavaScript关闭模式时,清除模式窗体模式、窗体、KnockoutJS、JavaScript

2023-09-03 21:51:34 作者:南斋孤云

我有一个引导模态包含用于更新或创建(在我的例子公司)的实​​体的形式。现在我的问题是,如果我使用了模态视图的实体,它不清除的领域时,我以任何方式关闭该模式。造成的形式仍然被填充,如果我再点击创建按钮,这应该给我一个空白模式。

我怎么能执行我的ViewModels的只是普通的JavaScript方法吗?这是的我的code部分的:

 功能视图模型(){
        无功自我=这一点;

       功能CompanyViewModel(公司){
            无功自我=这一点;
            self.Id = company.CompanyId;
            self.Name = company.Name;
        }

        功能BlankCompanyViewModel(){
            无功自我=这一点;
            self.Id = 0;
            self.Name =;
        }

        self.company = ko.observable();
        self.companies = ko.observableArray();


        self.clearCurrentCompany =功能(){
            self.company(新BlankCompanyViewModel());
        };

       //初始化视图模型
        $ .getJSON(/ API /公司,函数(公司){
            $每个(公司,函数(指数公司){
                self.companies.push(新CompanyViewModel(公司));
            });
            self.clearCurrentCompany();
        });
    }
 

在理想情况下,我想在像这样的模式的隐藏事件上运行ViewModel.clearCurrentCompany:

  $('#myModal')。在('隐藏',函数(){
       //做的东西在这里,不知道是什么
    });
 

解决方案

 功能视图模型(){
    无功自我=这一点;
    //你的previous code
    $('#myModal')。在('隐藏',函数(){
       self.clearCurrentCompany();
    });
}
 
前端面试送命题 JS三座大山

就这样。请注意,你要隐藏,不隐藏,因为火灾隐患的模式完全消失之后。如果用户打开一个创建previous视图关闭之前,它仍然会被填充。

I have a Bootstrap Modal that contains a form for updating or creating an entity (Company in my example). Right now my issue is that if I view an entity using the modal, it doesn't clear out the fields when I close the modal by any means. Causing the form to still be populated if I then click a "Create" button, which should bring me up a blank modal.

How can I execute one of my ViewModels methods from just regular javascript? Here is some of my code:

function ViewModel() {
        var self = this;

       function CompanyViewModel(company) {
            var self = this;
            self.Id = company.CompanyId;
            self.Name = company.Name;
        }

        function BlankCompanyViewModel() {
            var self = this;
            self.Id = 0;
            self.Name = "";
        }

        self.company = ko.observable();
        self.companies = ko.observableArray();


        self.clearCurrentCompany = function() {
            self.company(new BlankCompanyViewModel());
        };

       // Initialize the view-model
        $.getJSON("/api/company", function(companies) {
            $.each(companies, function(index, company) {
                self.companies.push(new CompanyViewModel(company));
            });
            self.clearCurrentCompany();
        });
    }

Ideally I'd like to run ViewModel.clearCurrentCompany on the "Hidden" event of the modal like so:

 $('#myModal').on('hidden', function() {
       //Do something here, not sure what
    });

解决方案

function ViewModel() {
    var self = this;
    // your previous code
    $('#myModal').on('hide', function() {
       self.clearCurrentCompany();
    });
}

Just like that. Note that you want hide, not hidden, because hidden fires only after the modal completely disappears. If a user opens a create before the previous view closes, it will still be populated.