I try to add UI Component to product item on product list due to difficult FE login for each item. But I was wondering when conflicts between components appeared. All components for each item has a unique name.
This is an example how I try to implement it:
Added div with scope to list.phtml and added script for each item:
<div class="test-component" data-bind="scope: 'test_<?= $_product->getId() ?>'">
<!-- ko template: getTemplate() --><!-- /ko -->
</div>
<script type="text/x-magento-init">
{
"*": {
"Magento_Ui/js/core/app": {
"components": {
"test_<?= $_product->getId() ?>": {
"component": "Magento_Catalog/js/test-component",
"template" : "Magento_Catalog/test-component",
"config": {
}
}
}
}
}
}
</script>
Then I added JS Component test-component.js
:
define([
'jquery',
'uiComponent',
'ko'
], function ($, Component, ko) {
return Component.extend({
testObservable: ko.observable(1),
initialize: function() {
this._super();
}
});
});
Finally added HTML template test-component.html
<div>
<input type="text"
data-bind='value: testObservable'
class="input-text qty" min="1"/>
</div>
As a result, fields display for each product and observable works but when I change it for one item it changes for all items:
The real example is much more complex but this is an example of a problem. Maybe UI components don't a good solution? As a solution, I can call some JS method on changing and set it to some observable object with the productId key but it looks weird when observable value shares between components.
Thanks.