I've encountered an issue while using knockout with the revealing module pattern. My goal is to pass an observable by reference and have its value based on a computed function. However, it seems like the observable is being passed in by value instead of reference.
The listPersonClientSelect variable is an observable(), and I want to pass it into a JavaScript function to establish a computed calculation based on changes in select2Data(). You can see in the code below that I am passing the observable as a parameter in the init function, but I want to set its value to the computed result.
The init function is called as follows:
self.assigneePersonSetupKO.listPersonClientSetup shows the computed result, but what I really need is for self.assignees() to reflect these changes (the value of the computed result).
Is there a way to pass self.assignees into the revealing module pattern in JavaScript by reference?
self.assigneePersonSetupKO = new PersonSetupKO();
self.assigneePersonSetupKO.init(self.assignees);
var PersonSetupKO = function () {
"use strict";
//var self = this;
var select2Data = ko.observable(''),
initialOptions = [],
initialSelectedOptions = [],
listPersonClientSelect = ko.observable(),
init = function (listPersonClientSelect) {
this.initialOptions = $.map(listPersonClientSelect(), function (item) {
return { DisplayName: item.DisplayName(), Gen: item.Id() }
});
this.initialSelectedOptions = $.map(listPersonClientSelect(), function (item) {
return item.Gen();
});
this.select2Data($.map(listPersonClientSelect(), function (item) {
return { text: item.DisplayName(), id: item.Id() };
});
this.listPersonClientSelect = ko.computed(function () {
var results = $.map(select2Data(), function (item) {
return {
DisplayName: ko.observable(item.text),
Id: ko.observable(item.id)
}
});
return results;
});
};
return {
init: init,
select2Data: select2Data,
initialOptions: initialOptions,
initialSelectedOptions: initialSelectedOptions,
listPersonClientSelect: listPersonClientSelect
};
};