-1

I want to load my page based on default item selected in the combobox and reload on changing selected item (knockout).

Currently what I am doing is loading the form on click of a button. Here is my code. Please suggest me as I am very new to knockout.

html:

<select data-bind="options : employeeList, optionsText : 'name', value : selectedEmployeeList"></select>
</span>
<button class="toolbar-button" data-bind="click : load, disable :loading">Load employee</button>

js:

var filterVM = function () {
    this.employeeList = ko.observableArray();
    this.selectedEmployeeList = ko.observable();
};

dataService.getEmployeeLists(this.viewData.EmployeeId).then(loadEmployeeLists).then(enableControls);

filterVM.prototype.load = function () {

    var selectedEmployeeList = this.selectedEmployeeList();
    var self = this;

    if (selectedEmployeeList) {
        disableControls();
        dataService.getEmployeeByEmployeeList(this.viewData.employeeId, selectedEmployeeList.id)
            .done(loadPEmployeeSpread);
    }
};
Rahul Pandey
  • 435
  • 1
  • 3
  • 13

1 Answers1

1

You can call your load function on change of selected value selectedEmployeeList Add following code in constructor

this.selectedEmployeeList .subscribe(function(newValue) {
    //This function is going to get called on every change of the selectedEmployeeList 
    //Add here your code to load the page
}, this);

subscribe :- is facility provided by knockout that it tracks every change made in the subscribed variable and calls function on every change.

Dnyanesh
  • 2,265
  • 3
  • 20
  • 17