Moving the Shopping Cart Module
I want to do one last thing. The shopping cart controller is mighty handy. No doubt we want to reuse this functionality in Node.js to create invoices. Luckily, we have kind of future proofed our frontend scripts with Browserify already, so we can simply create a module and use it on both the frontend and backend.
Create a new file in your scripts
folder and name it order.js
. In it goes most of the code from the shopping-cart.js
file:
module.exports = (function () { var Order = function () { this.lines = []; }; Order.prototype.removeLine = function (line) { this.lines.splice(this.lines.indexOf(line), 1); }; [...] return { Order: Order, Line: Line }; })();
Since we are not working on the $scope
object anymore, we need some new object to contain the Line
objects. So, we create an Order
constructor that has a lines
array. The total
function and the removeLine
function are put on Order prototype
. Please note that removeLine...