0

I've been experimenting with a custom module with KnockoutJs on the frontend in M2.

I've created two components: a ProductPackage and a ProductPackagePopop. Functionaly, the ProductPackage triggers a modal which renders some details about the package's children.

These two components share quite some logic which i would like to share between the components. For example: both have a function 'initTranslations' which retrieves the right translations for this store id.

Since i'm new to the magento2 Knockout implementation, is there some way to create a base component from which my two components inherit the shared logic? Or a trait like in PHP. I've looked at mixins, but it seems to me it's more fit to extend external is.

define(['uiComponent'], function (Component) {

    return Component.extend({
        store_id: 0,

        initialize: function (config) {
            this.store_id = config.store_id;
            this.initTranslations(config.productPackage);
        },
        initTranslations: function (productPackage) {
          //some logic here
        },
        children: function(){
         // retrieves children
        }


    });
});
  • 1
    I'm not seeing any knockout code in your question. What exactly do you expect as an answer? – Tomalak May 21 '21 at 16:00
  • 1
    ...also Magento is server-side, Knockout is client-side, these two things technically have no overlap. Knockout really does not care which stack the server runs, so I'm confused as to what's Magento's relevance to the question. – Tomalak May 21 '21 at 16:03
  • 1
    Hi @Tomalak: The reason i mention magento 2 is that it has its own wrapper around knockoutjs. Also see: https://stackoverflow.com/questions/16569810/knockout-viewmodel-base-class-javascript-inheritance I'my trying to include the example here: https://stackoverflow.com/questions/16569810/knockout-viewmodel-base-class-javascript-inheritance in the magento2 way of creating components/viewmodels. – user2594875 May 21 '21 at 20:02
  • For the M2 example, i meant this one: https://magento.stackexchange.com/questions/201859/magento-2-uicomponent-view-model – user2594875 May 21 '21 at 20:11
  • Ah, I see what you mean. – Tomalak May 22 '21 at 06:52
  • I've tried to dig into the documentation of M2, but there are way too many unknowns for me. [Magento Stack Exchange](https://magento.stackexchange.com/) might be the be the better site to ask this question after all. – Tomalak May 22 '21 at 07:42
  • Thanks, i'll try that! – user2594875 May 24 '21 at 09:37
  • Answer here: https://magento.stackexchange.com/questions/337604/inheritance-in-knockoutjs-components-within-magento-2s-knockout-framework – user2594875 May 24 '21 at 10:24
  • Nice! I actually had something like that, but I knew too little about Magento's internals to post it with any kind of confidence. – Tomalak May 24 '21 at 10:31

0 Answers0