Syed Amanulla
Syed Amanulla

Reputation: 29

Binding multiple viewmodel in knockout.js

I have a requirement to have the educational details and the employment history of an employee. So I decided to use Knockout JS for dynamic forms.

Here is my code:

    //Data
    var History = function () {
        var self = this;
        self.CompanyName = ko.observable();
        self.Designation = ko.observable();
        self.StartDate = ko.observable();
        self.EndDate = ko.observable()
    };

    var viewModelEmploymentHistory = function () {
        // Operations
        var self = this;
        self.historyList = ko.observableArray([new History()]); // Put one line in by default
        self.addHistory = function () { self.historyList.unshift(new History()) }
        self.removeHistory = function (history) {
            self.historyList.remove(history)
        }
    };

    //Data
    var Education = function () {
        var self = this;
        self.Degree = ko.observable();
        self.YearOfPassing = ko.observable();
        self.Percentage = ko.observable();
        self.Institute = ko.observable()

    };

    var viewModelEducationalDetails = function () {
        // Operations
        var self = this;
        self.educationList = ko.observableArray([new Education()]); // Put one line in by default
        self.addEducation = function () { self.educationList.unshift(new Education()) };
        self.removeEducation = function (education) {
            self.educationList.remove(education)
        }
    };

    //var masterVM = (function () {
    //    this.viewModelEducationalDetails = new viewModelEducationalDetails(),
    //    this.viewModelEmploymentHistory = new viewModelEmploymentHistory();
    //})();

    //ko.applyBindings(masterVM)
    //ko.applyBindings(new viewModelEmploymentHistory());

    ko.applyBindings(new viewModelEducationalDetails(), document.getElementById("containerEducation"));
    ko.applyBindings(new viewModelEmploymentHistory(), document.getElementById("containerHistory"));

HTML:

    <div  id="containerHistory" >    
    <div data-bind="foreach: historyList">             
        <div class=" col-md-9 col-lg-10 border ">
         <div ><a href="#" class="hrefRemove" data-bind='click: $parent.removeHistory'>Remove</a></div>          
            <div class="form-group"> 
                @Html.LabelFor(model => model.CompanyName, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">
                    @Html.TextBoxFor(model => model.CompanyName, new { @Class = "form-control", placeholder = "Company Name", data_bind = "value: CompanyName, namePath: true" })
                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.CompanyName) </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Designation, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">
                    @Html.TextBoxFor(model => model.Designation, new { @Class = "form-control", placeholder = "Designation", data_bind = "value: Designation, namePath: true" })
                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.Designation) </div>
            </div>
            <div class="form-group">
                @*@Html.HiddenFor(m=>m.DateOfBirth)*@
                @Html.LabelFor(model => model.StartDate, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">

                    @Html.EditorFor(model => model.StartDate, new { placeholder = "MM/DD/YYYY", @Class = "hasDatepicker dateClass", data_bind = "value: StartDate , namePath: true" })          
                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.StartDate) </div>
            </div>
            <div class="form-group">
                @*@Html.HiddenFor(m=>m.DateOfBirth)*@
                @Html.LabelFor(model => model.EndDate, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">
                    @Html.EditorFor(model => model.EndDate, new { placeholder = "mm/dd/yyyy", @Class = "hasDatepicker dateClass", data_bind = "value: EndDate, namePath:true" })

                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.EndDate) </div>
            </div>
        </div>
        <br />
        <br />

    </div>

        </div>




div id="containerEducation">

    <div data-bind="foreach: educationList">
        <div class=" col-md-9 col-lg-10 border ">
            <div><a href="#" class="hrefRemoveEducation" data-bind='click: $parent.removeEducation'>Remove</a></div>

            <div class="form-group">
                @Html.LabelFor(model => model.Degree, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">
                    @Html.TextBoxFor(model => model.Degree, new { @Class = "form-control", placeholder = "Degree Name", data_bind = "value: Degree, namePath: true" })
                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.Degree) </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Institute, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">
                    @Html.TextBoxFor(model => model.Institute, new { @Class = "form-control", placeholder = "Institute Name", data_bind = "value: Institute, namePath: true" })
                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.Institute) </div>
            </div>
              <div class="form-group">
                @*@Html.HiddenFor(m=>m.DateOfBirth)*@
                @Html.LabelFor(model => model.Percentage, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">

                    @Html.TextBoxFor(model => model.Percentage, new { @Class = "form-control", placeholder = "Percentage", data_bind = "value: Percentage, namePath: true" })

                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.Percentage) </div>
            </div>
            <div class="form-group">
                @*@Html.HiddenFor(m=>m.DateOfBirth)*@
                @Html.LabelFor(model => model.YearOfPassing, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">
                    @Html.TextBoxFor(model => model.YearOfPassing, new { @Class = "form-control", placeholder = "YearOfPassing", data_bind = "value: YearOfPassing, namePath: true" })

                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.YearOfPassing) </div>
            </div>
        </div>
        <br />
        <br />

    </div>

</div>

I get a console error as below when i try to add more div.

Uncaught Error: Unable to parse bindings. Message: ReferenceError:
historyList is not defined; Bindings value: foreach: historyList

Please note : This works fine when I bind a single view model.

Upvotes: 1

Views: 224

Answers (1)

Syed Amanulla
Syed Amanulla

Reputation: 29

This is the trick.

 var viewModelEmploymentHistory = function () {
            // Operations
            var self = this;
            self.historyList = ko.observableArray([new History()]); // Put one line in by default  
            self.addHistory = function () { self.historyList.unshift(new History()) }
            self.removeHistory = function (history) {
                self.historyList.remove(history)
            }
            self.educationList = ko.observableArray([new Education()]); // Put one line in by default  
            self.addEducation = function () { self.educationList.unshift(new Education()) };
            self.removeEducation = function (education) {
                self.educationList.remove(education)}
        };

Upvotes: 1

Related Questions