PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/WS.EKA.Portal/Scripts/models/myCart.js

http://mobileshop.codeplex.com
JavaScript | 205 lines | 186 code | 19 blank | 0 comment | 21 complexity | d8145100da19fb70a40d29abc069547e MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. window.MyCart = Backbone.Model.extend({
  2. urlRoot: 'api/shoppingcart/',
  3. url: function () {
  4. return this.urlRoot;
  5. }
  6. });
  7. window.MyCartList = Backbone.Collection.extend({
  8. model: MyCart,
  9. urlRoot: 'api/shoppingcart/',
  10. url: function () {
  11. return this.urlRoot + pageView.getCookieValue('uid');
  12. },
  13. parse: function (list) {
  14. _.each(list, function (model) {
  15. model['DiffPrice'] = model['MarketPrice'] - model['BuyPrice'];
  16. });
  17. return list;
  18. }
  19. });
  20. window.MyCartView = Backbone.View.extend({
  21. el: "#jqt",
  22. initialize: function () {
  23. this.collection = new MyCartList();
  24. this.template = $('#MyCartTemplate').html();
  25. },
  26. events: {
  27. 'click #goShopping,.backShopping': 'goShopping',
  28. 'click #MyCart .p-amount .add,#MyCart .p-amount .redu': 'changeProductCount',
  29. 'change .productcount': 'updateProductCount',
  30. 'click #MyCart .p-amount .delete': 'deleteFromCart',
  31. 'click #MyCart li .p-img,#MyCart li .p-name': 'renderProductDetail',
  32. 'click #MyCart #ordering': 'checkOrder'
  33. },
  34. render: function () {
  35. var partial = { header: $('#HeaderTemplate').html(), footer: $('#FooterTemplate').html(),
  36. innerFooter: $('#InnerFooterTemplate').html()
  37. };
  38. var data = { title: '???', btnListR: [{ name: 'sort' }, { name: 'cart' }, { name: 'home'}] };
  39. this.$el.append(Mustache.render(this.template, data, partial));
  40. this.fetchCarts();
  41. return this;
  42. },
  43. fetchCarts: function () {
  44. var that = this,
  45. loginId = localStorage.getItem('loginId'),
  46. template = $('#CartContentTemplate').html(),
  47. data,
  48. partial = { cartTemplate: $('#NoEmptyCartTemplate').html(), emptyCartTemplate: $('#EmptyCartTemplate').html() };
  49. that.collection.fetch({
  50. success: function () {
  51. var carts = that.collection.toJSON();
  52. if (carts == null || carts.length == 0) {
  53. data = { hasCart: false };
  54. localStorage.setItem('hasCarts', false);
  55. } else {
  56. localStorage.setItem('hasCarts', true);
  57. localStorage.setItem('cartItems', JSON.stringify(carts));
  58. var count = 0, allPrice = 0.0;
  59. _.each(carts, function (cart) {
  60. count += +cart['BuyNumber'];
  61. allPrice += (+cart['BuyNumber']) * (+cart['BuyPrice']);
  62. });
  63. data = { hasCart: true, mycarts: carts, count: count, allPrice: allPrice };
  64. localStorage.setItem('allPrice', allPrice);
  65. }
  66. $('#cartContent').html(Mustache.render(template, data, partial));
  67. pageView.changeCartIcon();
  68. pageView.resizeScroll();
  69. }
  70. });
  71. },
  72. changeProductCount: function (e) {
  73. var target = $(e.currentTarget),
  74. that = this,
  75. count,
  76. tagName = target[0].tagName.toLowerCase(),
  77. productid = target.closest('li').attr('productid'),
  78. price = +target.closest('li').find('.pro-price').text(),
  79. type = target.hasClass('add') ? 'add' : 'reduce',
  80. input = target.siblings('input.productcount');
  81. var currentCount = +input.val();
  82. if (type === 'add') {
  83. input.val(currentCount + 1);
  84. count = 1;
  85. }
  86. else {
  87. if (currentCount == 1) return;
  88. input.val(currentCount - 1);
  89. count = -1;
  90. }
  91. window.setTimeout(function () {
  92. MyCartView.addToCarts(productid, price, count, function () {
  93. that.fetchCarts();
  94. });
  95. }, 300);
  96. },
  97. updateProductCount: function (e) {
  98. var target = $(e.currentTarget),
  99. that = this,
  100. productid = target.closest('li').attr('productid'),
  101. count = +target.val();
  102. if (isNaN(count)) return;
  103. if (count < 1) { this.deleteFromCart(e); return; }
  104. window.setTimeout(function () {
  105. MyCartView.UpdateToCarts(productid, count, function () {
  106. that.fetchCarts();
  107. });
  108. }, 300);
  109. },
  110. deleteFromCart: function (e) {
  111. var that = this,
  112. target = $(e.currentTarget),
  113. productid = target.closest('li').attr('productid');
  114. MyCartView.deleteProduct(productid, function () { that.fetchCarts(); });
  115. },
  116. goShopping: function () {
  117. pageView.goTo('FirstCategory');
  118. },
  119. renderProductDetail: function (e) {
  120. var target = $(e.currentTarget),
  121. productid = target.closest('li').attr('productid'),
  122. price = target.closest('li').find('span.pro-price').text();
  123. localStorage.setItem('productid', productid);
  124. localStorage.setItem('productDetail', JSON.stringify({ productid: productid, price: price }));
  125. pageView.goTo('ProductDetail');
  126. },
  127. checkOrder: function () {
  128. localStorage.removeItem('couponitem');
  129. pageView.goTo('OrderMake');
  130. }
  131. }, {
  132. addToCarts: function (productid, price, count, callback) {
  133. var cart = new MyCart();
  134. cart.set({
  135. MemLoginID: pageView.getCookieValue('uid'),
  136. ProductGuid: productid,
  137. BuyNumber: +count,
  138. BuyPrice: +price,
  139. Attributes: '??',
  140. ExtensionAttriutes: 'M'
  141. });
  142. cart.save('', '', {
  143. success: function (mdoel, res) {
  144. if (res && res == 202) {
  145. callback.call();
  146. }
  147. }
  148. });
  149. },
  150. UpdateToCarts: function (productid, count, callback) {
  151. var cart = new MyCart();
  152. cart.set({
  153. id: 1,
  154. MemLoginID: pageView.getCookieValue('uid'),
  155. ProductGuid: productid,
  156. BuyNumber: +count,
  157. Attributes: '??',
  158. ExtensionAttriutes: 'M'
  159. });
  160. cart.save('', '', {
  161. success: function (mdoel, res) {
  162. if (res && res == 202) {
  163. callback.call();
  164. }
  165. }
  166. });
  167. },
  168. deleteProduct: function (productid, callback) {
  169. if (confirm('??????')) {
  170. var loginid = pageView.getCookieValue('uid');
  171. var mycart = new MyCart({
  172. id: 1,
  173. MemLoginID: loginid,
  174. ProductGuid: productid
  175. });
  176. mycart.url = mycart.urlRoot + loginid + '/' + productid;
  177. mycart.destroy({
  178. success: function (model, res) {
  179. if (res && res == 200) {
  180. callback.call();
  181. }
  182. }
  183. });
  184. }
  185. }
  186. });