Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
Need help deleting multiple container instances with onReplicantDelete
System
I have a form with a container that is only allowed to have 1 or 4 instances (never 2 or 3).
If the user clicks to add an instance, I used the onReplicantAdded and it adds three. Easy enough.
Now, if the user clicks to delete instance 1 or 2 or 3 or 4, I tried to use the onReplicantDelete to delete instances 2, 3 and 4 (regardless if they clicks on 1 or 2 or 3 or 4). But, this enters me into an infinite loop. Best guess is that the function is getting retriggered when I'm using .deleteInstance within the function. But, I don't know how to get around that.
I'm sure someone has had this scenario before. Is there any advice you can give me to help me figure out how to avoid this infinite loop?
Here's a very simple code layout:
function init() {
IWEventRegistry.addItemHandler("/featured_products_banner/product","onReplicantDelete",fnHandleProdLinkDelete);
}
function fnHandleProdLinkDelete() {
IWDatacapture.getItem("featured_products_banner/product").deleteInstance(2);
IWDatacapture.getItem("featured_products_banner/product").deleteInstance(3);
IWDatacapture.getItem("featured_products_banner/product").deleteInstance(4);
return false;
}
I'm new to TeamSite FormAPI so I apologize if this is a common question.
Find more posts tagged with
Comments
nipper
A couple of points (may not be the problem you have, but still)
in your function, you are missing the initial / for the featured_products_banner/product item
ALso, you probably should remove in order 4,3,2 instead of 2,3,4. If you remove #2, the 3 becomes 2 and 4 becomes 3. Second delete leaves #3 as now #2 and the 3rd delete may fail.
Migrateduser
I added the initial slash and it stopped working completely which is odd but something I've seen on my other template scripts. Not sure I understand why though. But, yes, I agree that is an inconsistency with my coding.
I also reversed the order of the instance deletion and still have the same problem. Even tried with just one of the instances (fourth) and still got stuck in the loop.
One thing I was thinking of trying is to use the onReplicantDelete to delete only the 1st. Then, create a new event in the init function for onReplicantMoved to monitor when a 2 instance is moved to 1. But, I still need to figure out how to code that. Also, seems like a lot of work for such a simple problem...(?)
nipper
OK, I think you need to rethink this procedure.
onReplicantDelete runs and expects a return true/false should TS delete the replicant or not.
So maybe removeItemHandler(...)
deleteInstance(...)
addItemHandler
return false - I am not certain if you would remove the one the user was trying to remove or not, so play with return true and return false.
BirtSQLError.JPG
Migrateduser
Thank you for the direction - hadn't thought of it from that perspective. And, EVEN BETTER, it worked!
function init() {
IWEventRegistry.addItemHandler("/featured_products_banner/product","onReplicantDelete",fnHandleProdLinkDelete);
}
function fnHandleProdLinkDelete() {
IWEventRegistry.removeItemHandler("/featured_products_banner/product","onReplicantDelete");
IWDatacapture.getItem("/featured_products_banner/product").deleteInstance(4);
IWDatacapture.getItem("/featured_products_banner/product").deleteInstance(3);
IWDatacapture.getItem("/featured_products_banner/product").deleteInstance(2);
IWEventRegistry.addItemHandler("/featured_products_banner/product","onReplicantDelete",fnHandleProdLinkDelete);
}
Simple. Love it! Thank you very much!