I created a component named Upload
that enables users to upload files and receive a JSON object containing these files. In this specific case, the Upload
component has an input from a parent view model:
<upload params="dropzoneId: 'uploadFilesDropzone', postLocation: '/create/upload', uploadedFiles: uploadedFiles"></upload>
The crucial parameter here is called uploadedFiles
. By binding this parameter, I am able to access params.uploadedFiles
in my component and use the .push()
method to add new objects as they are uploaded. The data passed as uploadedFiles
is actually an observableArray on my parent view model:
var UploadViewModel = function () {
// Files waiting to be added to the queue.
self.uploadedFiles = ko.observableArray([]);
};
I have verified that params.uploadedFiles
in my component is indeed an observableArray, as it supports the push method. Despite changing this value in the component, when I console.log()
it, there seems to be no update:
params.uploadedFiles.push(object);
console.log(params.uploadedFiles().length); // was 0, now it should be 1
The issue lies in the fact that this modification does not affect the array in my parent view model. The self.uploadedFiles()
still displays a length of 0.
Even after adding a
self.uploadedFiles.subscribe(function(newValue) {});
subscription in the parent view model, the problem persists.
Similarly, implementing a
params.uploadedFiles.valueHasMutated()
method in the component post alteration did not resolve the issue.
How can I ensure that changes made to the array in my component are reflected in the array within the parent view model?