Hi there, I'm looking to implement a customisation...
# suitecommerce
h
Hi there, I'm looking to implement a customisation to remove a child view from the cart lines depending on that line's item information. The problem (as I can see it) is that the cart lines are rendered in a Backbone Collection View, so there isn't a specific data-view for each individual line to be able to reference as the parent of the line's child view I want to remove. Is there a way to achieve this behaviour via the extensibility API? I'm currently imagining the code will look a bit like this:
Copy code
var cart = container.getComponent('Cart');

if (!cart) return;

cart.on('beforeShowContent', function(view)
{
	if (view !== cart.CART_VIEW) return;

	cart.getLines().then(function(lines)
	{
		_.each(lines, function(line)
		{
			var product = line && line.item || {},
				item = product.extras || {};

			if (!item.ispurchasable) return;

			cart.removeChildView(
				'Item.ListNavigable [data-item-id="' + product.internalid + '"]',	// data-view or some form of element selector for the line I want to adjust
				'StockDescription'													// name of data-view to remove
			);

		});
	});
});
s
So, the key thing to keep in mind here is that
removeChildView
works on the parent view class and not instances. By conditionally triggering it, you're essentially deciding whether to remove it from all instances of the view and not just some of them.
There is no extensibility API method that can be used to conditionally remove child view instances. Last time I checked, the best practice was to use jQuery to target and remove unwanted instances of child views once they had been rendered.
Rather than
cart.removeChildView
you would do something like:
jQuery('Item.ListNavigable [data-item-id="' + product.internalid + '"]').remove()
h
Thanks @Steve Goldberg - I did explore this option (though to be fair not in depth), but I found that the child view would be removed via jQuery and then added back after a couple of seconds. I believe this was because it was being re-rendered further down the data-view tree by a model being updated. In the end I reverted to extending the Cart.Lines.View, to override the function that adds the child view to insert my own logic into that function instead. This got my desired behaviour. Maybe it's my way of thinking (and being used to the older way of doing things), but I find the extensibility API rarely able to be leveraged for the customisations required by our clients.
k
You could always set the value in the view and use a handlebars condition in the template..?