Removing a product from the cart

The action to remove a product from the cart is triggered when the X button related to a cart entry is clicked. This is implemented in the removeFromCart() method, located in the CartLine class. This is similar to the method we used in the previous section. The code is as follows:

private def removeFromCart() = 
() => UIManager.deleteProduct(product)

We delegate the action to UIManager, and the implementation is as follows:

def deleteProduct(product: Product): JQueryDeferred = {
def onDone = () => {
val cartContent = $(s"#cart-${product.code}-row")
cartContent.remove()
println(s"Product ${product.code} removed from the cart")
}

deletefromCart(product.code, onDone)
}

This time, we call the deleteFromCart method and remove the row with the related ID.

The implementation of the web service call is as follows:

private def deletefromCart(
productCode: String,
onDone: () => Unit) = { val url = s"${UIManager.origin}/v1/cart/products/$productCode" $.ajax(JQueryAjaxSettings.url(url).method("DELETE")._result) .done(onDone) }

As jQuery does not have a delete() method, we have to use the ajax() method and set the HTTP method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset