OJET: Adding a JET Component programmatically

Recently I have been asked of one of my team colleagues about how we can generate some Oracle JET components once the View Model has been loaded.

GitHub: ojet-add-jet-comp-programmatically


Researching a bit in Knockout / JET and how the Binding Context works I found the following approach.

In the transitionCompleted lifecycle method, the DOM has already been generated, so we can use JavaScript API to get the DOM component what will we use to add Oracle JET Components.

By using insertAdjacentHTML method, we will add our Oracle JET Components.
In addition, you can already bind the methods / attributes to Knockout Obvervables if required.

 self.transitionCompleted = function () {
        // Implement if needed
        const fakeDiv = document.getElementById('fake');
        fakeDiv.insertAdjacentHTML('beforeend','');
        fakeDiv.insertAdjacentHTML('beforeend','DesktopLaptopTablet' )
        ko.cleanNode(fakeDiv);
        ko.applyBindings(this,fakeDiv);
      };

As you can see in the code, we are cleaning the DIV context and applying the bindings again, this is to avoid the Knockout Exception about applying bindings more than once and also to allow the new components to re-bind.

Comments

Popular posts from this blog

OJET: Inter-Module communication in TypeScript Template

OJET: Build and Deploy in an Application Server

OJET: Select All options using only Checkboxset