Search code examples
apostrophe-cms

How to call a piece function from another piece


I have the pieceA that have a joinbyarray relationship with pieceB. What a I want to do is to access to the functions of pieceB in the index.js of pieceA.

I know how to access to pieceB and its fields, but I don't know how to access to their functions.

Thanks in advance!


Solution

  • You have to make pieceB's interesting function a method on self in index.js.

    in lib/modules/piece-b/index.js

      construct: function (self, options) {
        self.coolThing = function () {
          return true;
        };
      }
    

    Then in pieceA's index.js you can access self.apos.modules['piece-b'].coolThing.

    If you've given pieceB an alias property you can access it straight away as self.apos.modules.pieceBsAlias