Incorporating Knockout.js into my single-page application has brought up a challenge for me. I am looking to transfer the value of one viewmodel data to another viewmodel data in order to minimize duplications in creating the same view. Below is a snippet from my codebase: I have two separate JavaScript files, one containing the Employee ViewModel and the other the Department ViewModel.
//Employee View
<div class="Employee-view" data-role="view" id="employee">
<div data-role="content" >
<ul>
<li foreach:EmployeeData>
//OnClick of this element should navigate to the Department view and bind all values based on a particular Employee ID
<div databind:"click:GetDepartmentDetails">
<span data-bind:"text:EmployeeId"> <span>
<span data-bind:"text:EmployeeName"> <span>
<span data-bind:"text:EmployeeImage"> <span>
</div>
</li>
</ul>
</div>
</div>
EmployeeViewModel = new EmployeeDetailsViewModel();;
$(".Employee-view").each(function () {
ko.applyBindings(EmployeeViewModel, $(this).get(0));
});
function EmployeeViewModel(){
var self=this;
self.EmployeeData = ko.observableArray([]);
self.GetEmployee = function(UserName,Password){
var UserModel = { UserName: UserName, Password: Password}
$.ajax({
type: "POST",
dataType: "json",
url: serverUrl + 'xxx/xxx/GetEmployee',
data: UserModel,
success: function (data) {
self.EmployeeData($.map(data, function (item) {
return new EmployeeModel(item);
}))
}
});
}
//Click EVENT
self.GetDepartmentDetails=(EmployeeData){
// I have access to all the values in this ViewModel; now I need to pass these values to the DepartmentViewModel and call the required function.
self.GetEmployeeByDepartment();
}
}
function EmployeeModel(data)
{
var self = this;
self.EmployeeId = ko.observable(data.EmployeeId)
self.EmployeeName = ko.observable(data.EmployeeName)
self.EmployeeImage = ko.observable(data.EmployeeImage)
}
//Department View
<div class="Department-view" data-role="view" id="Department">
<div data-role="content">
<ul>
<li data-bind:"foreach:DepartmentData ">
<div>
<span data-bind:"text:DeptId"> <span>
<span data-bind:"text:DeptName"> <span>
</div>
</li>
</ul>
</div>
</div>
//Department View Model
function DepartmentViewModel ()
{
var self = this;
self.DepartmentData = ko.observableArray([]);
self.GetEmployeeByDepartment = function(item){
employeeID = item.EmployeeId
employeename = item.Employeename
var DeptModel = { Employeename: employeeID, Employeename: employeename}
$.ajax({
type: "POST",
dataType: "json",
url: serverUrl + 'xxx/xxx/GetDepratmenDetails',
data: DeptModel ,
success: function (data) {
self.DepartmentData ($.map(data, function (item) {
return new DepartmentModel(item);
}),
});
}}
}
function DepartmentModel(data)
{
var self=this;
self.DeptId = ko.observable(data.DeptID)
self.DeptName = ko.observable(data.DeptName)
}
DepartmentViewModel = new DepartmentDetailsViewModel();
$(".Department-view").each(function () {
ko.applyBindings(DepartmentViewModel, $(this).get(0));
});
I'm facing an issue with duplication as we were unable to resolve it efficiently. Can someone assist? How to carry the data between different viewmodels in Knockout JS?