Click here to Skip to main content
14,603,943 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have an e-commerce site and want clicking of an item to reflect on both the item and a cart pegged at the top of the page. This works fine. I also want to maintain state across pages, such that if the server returns an already loaded cart, all corresponding items from that cart on the current page get selected. Attempts to ‘re-select’ these items throws the error Uncaught RangeError : Maximum call stack size exceeded.

What I have tried:

I understand the interpreter is complaining about the recursive process of trying to unselect cart items, reselect them in the items list ad infinitum. I’m trying to work around this by first removing all the cart items like so:
selectClones = function () {

		var retain = [];
	 	
	 	if (location.search.match(/cid=(\d+)/)) $('aside li').each((i,e) => {

	 		var e = $(e), team = e.find('span:first').text(), sel = e.find('span:last').text(),

	 		selInd = $(`th:contains(${sel})`).index();

	 		retain.push({t: team, s: selInd});
	 	}).remove();

	 	for (var i = retain.length - 1; i >= 0; i--) {
	 		
	 		$(`td:contains(${retain[i].t})`).parent().children().eq(retain[i].s).trigger('click');
	 	}
	}


Surprisingly, it doesn’t work unless I run this same snippet in the developer console. What else can I try?

The click listener

$('main').on('click', 'td.pointer, aside li.pointer', function(e) {

		if ($(e.target).is('td') && !$(e.target).hasClass('selected')) addSelection().call(this, null);

		else removeSelection().call(this, null);
	});


addSelection is a huge function that basically checks if a row of this item is preselected and tries to deselect it (this condition is what throws the error), then logs a custom object with details pertaining to this selected item for synchronization between the table list and the aside cart. I think this is more straightforward than posting the actual code but if that’ll make things clearer, let me know so I can post it too. Many thanks.
Posted
Comments
F-ES Sitecore 17-Sep-18 5:13am
   
Something in your code is probably in an infinite loop. Add some debugging lines or step through your code to work out what.
nmeri17 17-Sep-18 5:44am
   
I've solved it. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100