0

I have added a custom step in checkout where we list all the uploaded prescription by the customers and they can upload new file also. So once the customer upload the file, on "Next step" we upload that file using ajax. Till now everything is working fine.

But now, I want to add newly uploaded prescription in above list. Below is my upload code:

fileUploader.fileupload('send', data).success(function (result) {
            tmpInput.remove();
            result = JSON.parse(result);
            if ('error' in result && result['error'] != 0) {
                messageList.addErrorMessage({
                    message: $t('Something went wrong. Please contact Support team.')
                });
            } else {
                self.prescriptions.push(result);
                ko.cleanNode($('#prescription_list')[0]);
                ko.applyBindings(self, $('#prescription_list')[0]);
                messageList.addSuccessMessage({
                    message: $t('Prescription have been uploaded successfully.')
                });
            }
        }).error(function (e, data) {
            if (e.status !== 200) {
                messageList.addErrorMessage({
                    message: $t('Something went wrong. Please contact Support team.')
                });
            }
        }).done(function () {
            stepNavigator.next();
        });

Main Issue: Once the file is uploaded successfully and applyBinding executes, it add additional rows in the list. (For eg: in place of 5 rows in list it shows 20 rows).

1 Answer 1

1

You should not run applyBinding or cleanNode when writing KO within Magento 2. They have some dark magic that runs this somewhere in the core.

4
  • Then what should be the solution for adding dynamic rows on ajax complete? Oct 1, 2021 at 12:59
  • You need to create an observable array and update that on ajax complete, updating an observable will update the frontend. See knockoutjs.com/documentation/observableArrays.html
    – Ben Crook
    Oct 1, 2021 at 13:10
  • 1
    Your answer helped me to think on different direction and I have solved my issue. Thanks Oct 3, 2021 at 10:21
  • I am trying to add rows dynamically using ajax magento admin ui form. Please help
    – Liz Eipe C
    May 10, 2022 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.