ngCart
ngCart copied to clipboard
Change event firing twice when adding new item or adjusting quantity
The ngCart:change event is getting fired twice when you add a new item to the cart or adjust the quantity of an item already added. This makes it troublesome and inefficient to use this event for things such as syncing to an api.
This is happening for two separate, but related, reasons:
- Adding a New Item:
In
addItem,var newItem = new ngCartItem(id, name, price, quantity, data);callssetQuantityin the factory constructor, which broadcasts the change event before the item is actually created. It is then fired again at the end of the function. - Adjusting the Quantity:
Again in
addItem,inCart.setQuantity(quantity, false);fires the event first, and it is fired again at the end of the function.
This is causing problems for me too. I did a quick build with line 266 of ngCart.js removed and it solves my issues. The project I'm working on however uses bower to bring in dependencies and so I can't roll out this solution.
As far as I can tell through my experiences using ngCart (so far) I can't see how removing the broadcast at ngCart.js:266 would cause any problems, especially as it fires again on completion. Would there be a consideration to officially remove this?
The other option would be to perhaps feed through further information on the broadcast as currently the data object is empty. Having some kind of value identifying the nature of the change could provide the power to do more with the feature.