I am working on a custom extension which adds a ch...
# suitecommerce
z
I am working on a custom extension which adds a child view to Product Detail Page. The child view is showing something similar to related items section. The problem is, when I go from one item page to another, The child views get appended. I can see the child view from previous PDP in current PDP. Why is this happening and how to deal with this? Any help is appreciated.
s
It's probably due to how you're initialising your code. This would happen, for example, if you tell addChildView() to trigger when a page loads, rather than the application itself.
A common misunderstanding of addChildView() is that it is for adding a new instance rather than a class.
g
Yep, adding to what Steve said. The code that shows or changes the items need to be in the View, not where you execute the addChildView(). The code in the mountToApp function won't execute again when you change pages. The code inside the view will.
s
Indeed. If you need to change the content of the view, because it changes per-product, for example, then you will need to add a listener so that the view is re-rendered when its data changes
But generally that shouldn't be required if you set it up properly
For example, I have seen some people who want to render some content based on the matrix option selection – or, in other words, so the content changes depending on which child product is currently selected. In that case you would probably need to re-render the child view because the user isn't navigating away from the page, which causes the parent view to re-render.
z
addChildView function is called on beforeShowContent event of PDP. The data that is redered by template is all managed in View file. I am only doing the fetch operation in entry point file.
I am assuming I should move the fetch function to view file itself.
g
Yeah, I would move the fetch to the initialize in the View. Also is there a reason you're doing the addChildView inside the beforeShowContent?
s
No, you should move the addChildView to the entry point file
That is the cause of the problem. Every time showContent is about to be called (which is often), it will create a new child view class
If you want to do that then you will need to manage that yourself, ie by destroying them when you're done with them. It is, however, not good practice and much easier to simply put addChildView in your entry point file
z
This is in my entrypoint file. I have moved the fetch operation to child view file. Now, The fetch operation is giving back error now. I am investigating that. Does this look okay?
s
You don't need the beforeShowContent
z
I tried without it too. It gives itemId as null.
g
the code to get the item id can be placed inside the function for "addChildView"
s
Yes because the way your code has been written doesn't take into account how the application is loading and what data is available at each point
z
Okay, I understand what you are saying. I'll make some changes.
g
Copy code
pdp.addChildView('BuildYourCollection.View', function () {
    var itemInfo = pdp.getItemInfo();
    var itemId = itemInfo.item.internalid;
    ....
})
but you don't need the beforeShowContent
s
I don't think you should need to (and I don't think it'll work to) pass in the itemID in the entry point file or in the constructor, you can just put that in your view
getItemInfo() should only work if you're in a PDP view (parent or child)