Identifier ViewModel has already been declared

Viewed 10

I am using knockout JS in a bootstrap modal and getting 'SyntaxError': Identifier AddDishViewModel has already been declared.

This error shows up only after opening the modal from the second time. Question is how do I prevent AddDishViewModal from being redeclared upon opening the modal the second time. Thanks

Razor:

@model Web.ViewModels.ViewModel

<div id="add-dish" class="modal-content">
    <span id="section-id">@Model.SectionId</span>
    <button type="button" data-dismiss="modal" aria-label="Close" data-bind="click: cleanNode">
        <i class="fas fa-times"></i>
    </button>
    <div data-bind="visible: showMainLoader">
        @Html.Partial("_Loader")
    </div>
</div>

<script src="~/js/script.js" type="text/javascript"></script>

script.js

"use strict";
class AddDishViewModel{
    constructor() {
        this.node = document.getElementById("add-dish")
        this.sectionId = Number($('#section-id').text())
        this.pageSize = 12
        this.showMainLoader = ko.observable(true)


        
        ko.applyBindings(this, this.node)
    }

    this.cleanNode = () => {
        if (this.node) {
            ko.cleanNode(this.node)
        }
    }
}


$(() => {
    new AddDishViewModel();
})
0 Answers
Related