JavaScript KnockoutJS/Bootstrap - Clearing modal form when closing modal using javascript

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
    });
Answer:1
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.

Answer:2

I'm trying to write a benchmark, using benchmark.js, for some popular node.js unzip libs, mainly adm-zip, yauzl & unzip. However, I'm not sure if I've written the benchmark code correctly, because ...

I'm trying to write a benchmark, using benchmark.js, for some popular node.js unzip libs, mainly adm-zip, yauzl & unzip. However, I'm not sure if I've written the benchmark code correctly, because ...

I hope this question is not confusing or too complex. but here we go: I'm creating a custom google map that renders some US states as polygon overlays with the "google.map.Polygon' method. Everything ...

I hope this question is not confusing or too complex. but here we go: I'm creating a custom google map that renders some US states as polygon overlays with the "google.map.Polygon' method. Everything ...

I'm looking for a way to, from a backbone collection, retrieve some kind of array of one specific attribute. var SomeModel = Backbone.Model.extend({ defaults: function() { return { ...

I'm looking for a way to, from a backbone collection, retrieve some kind of array of one specific attribute. var SomeModel = Backbone.Model.extend({ defaults: function() { return { ...

To see the result, open console in Google Chrome, do the following: obj = new Boolean(false) "obj && true: " + (obj && true) "obj || true: " + (obj || false) and: (obj && ...

To see the result, open console in Google Chrome, do the following: obj = new Boolean(false) "obj && true: " + (obj && true) "obj || true: " + (obj || false) and: (obj && ...

  1. javascript snake
  2. javascript foreach
  3. javascript map
  4. javascript for loop
  5. javascript array
  6. javascript substring
  7. javascript switch
  8. javascript reduce