angular.module('CartService', ['ObserverService', 'AjaxService', 'MapService']).service('Cart', function (Observer, Ajax, $q, Map) { var obCart = Observer.create(); var local_restaurant_id = null; var cart_defer = null; this.getOb = function () { return obCart; }; this.refreshCart = function () { cart_defer = null; obCart.notify(); }; this.getCart = function (restaurant_id) { if (!cart_defer) { cart_defer = $q.defer(); var defer = cart_defer; Ajax.get("Cart", "getCart", [restaurant_id], function (response) { if (restaurant_id) { local_restaurant_id = restaurant_id; } defer.resolve(response); }); } return cart_defer.promise; }; this.addCart = function (item_id, restaurant_id) { var defer_operation = $q.defer(); Ajax.get("Cart", "addCart", [item_id, restaurant_id], function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; this.clearCart = function (restaurant_id) { var defer_operation = $q.defer(); Ajax.get("Cart", "clearCart", [restaurant_id], function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; this.removeCart = function (index, restaurant_id) { var defer_operation = $q.defer(); Ajax.get("Cart", "removeCart", [index, restaurant_id], function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; this.setVariations = function (variations, index, restaurant_id) { var defer_operation = $q.defer(); Ajax.post("Cart", "setVariations", [index, restaurant_id], {variations: variations}, function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; this.setIngredients = function (ingredients, index, restaurant_id) { var defer_operation = $q.defer(); Ajax.post("Cart", "setIngredients", [index, restaurant_id], {ingredients: ingredients}, function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; this.setGift = function (gift_id, index, restaurant_id) { var defer_operation = $q.defer(); Ajax.get("Cart", "setGift", [gift_id, index, restaurant_id], function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; this.removeGift = function (index, restaurant_id) { var defer_operation = $q.defer(); Ajax.get("Cart", "removeGift", [index, restaurant_id], function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; this.addVariation = function (item_id, index, restaurant_id) { var defer_operation = $q.defer(); Ajax.get("Cart", "addVariation", [item_id, index, restaurant_id], function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; this.addIngredient = function (item_id, index, restaurant_id) { var defer_operation = $q.defer(); Ajax.get("Cart", "addIngredient", [item_id, index, restaurant_id], function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; this.addTextVariation = function (text, index, restaurant_id) { var defer_operation = $q.defer(); Ajax.post("Cart", "addTextVariation", [index, restaurant_id], {text: text}, function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; this.removeVariation = function (index, variation_index, restaurant_id) { var defer_operation = $q.defer(); Ajax.get("Cart", "removeVariation", [index, variation_index, restaurant_id], function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; this.removeIngredient = function (index, ingredient_index, restaurant_id) { var defer_operation = $q.defer(); Ajax.get("Cart", "removeIngredient", [index, ingredient_index, restaurant_id], function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; this.hasCart = function (cart) { return cart && cart.items && cart.items.length > 0; }; this.setNote = function (text, restaurant_id) { var defer_operation = $q.defer(); Ajax.post("Cart", "setNote", [restaurant_id], {text: text}, function (response) { local_restaurant_id = restaurant_id; defer_operation.resolve(response); defer_operation = null; cart_defer = null; obCart.notify(); }); return defer_operation.promise; }; });