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.
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.
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',' ' ) ko.cleanNode(fakeDiv); ko.applyBindings(this,fakeDiv); }; Desktop Laptop Tablet
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
Post a Comment