[关闭]
@2890594972 2018-04-13T06:02:20.000000Z 字数 1730 阅读 708

面向对象重构购物车重构-02

面向对象 1511


面向对象重构购物车-结构代码

  1. Array.prototype.remove = function (val) {
  2. var index = this.indexOf(val);
  3. if (index > -1) {
  4. this.splice(index, 1);
  5. }
  6. };
  7. // 产品对象
  8. function Product(id, name, price, num, shopId){
  9. this.id = id;
  10. this.name = name;
  11. this.price = price;
  12. this.num = num;
  13. this.shopId = shopId;
  14. }
  15. // 购物车对象
  16. // 用来做什么: 管理数据,实现增删改查
  17. /*
  18. * 管理数据,有增删改查
  19. * add, delete, edit, get.
  20. * isExit
  21. **/
  22. // 用来做什么: 管理事件,用来监听交互事件
  23. /**
  24. * initEvent
  25. * 店铺单个: check
  26. * 店铺全选: checkAll
  27. * 全选: checkTotalAll
  28. * 增加和减少: plus, minus
  29. */
  30. // 用来做什么: 管理UI, 响应UI, 用来更新数据
  31. /**
  32. * 更新店铺价: uiShopTotalPrice,
  33. * 更新所有店铺: uiAllTotalPrice
  34. * 删除页面节点: uiDelete
  35. **/
  36. function Shopcar(){
  37. // 产品列表
  38. this.productList = [];
  39. }
  40. /*添加产品数据*/
  41. Shopcar.prototype.add = function(pro){
  42. this.productList.push(pro);
  43. }
  44. /*删除产品数据*/
  45. Shopcar.prototype.delete = function(id){
  46. var obj = this.get(id);
  47. this.productList.remove(obj);
  48. }
  49. /*获取产品数据*/
  50. Shopcar.prototype.get = function(id){
  51. // obj
  52. var obj = null;
  53. for(var i=0; i< this.productList.length; i++){
  54. if(this.productList[i]['id'] === id){
  55. obj = this.productList[i];
  56. }
  57. }
  58. return obj;
  59. }
  60. /*修改产品数据*/
  61. Shopcar.prototype.edit = function(id,pro){
  62. // 获取对象
  63. var obj = this.get(id);
  64. // 修改对象
  65. obj.num = pro.num;
  66. }
  67. /*创建产品*/
  68. ShopCar.prototype.createProduct = function ($shopInfo) {
  69. var shopId = $shopInfo.parents('.shop-group-item').data('id');
  70. var id = $shopInfo.data('id');
  71. var name = $shopInfo.find('h4').text();
  72. var num = +$shopInfo.find('.num').text();
  73. var price = +$shopInfo.find('.price').text();
  74. var pro = new Product(id, name, price ,num , shopId);
  75. return pro;
  76. }
  77. /*判断商品是否存在*/
  78. ShopCar.prototype.isExit = function(id){
  79. return this.get(id) === null ? false: true;
  80. }
  81. // 接近于MVC思想
  82. /*管理事件*/
  83. Shopcar.prototype.initEvent = function(){
  84. var that = this;
  85. // 选中产品
  86. $(".goodsCheck").click(function(){
  87. })
  88. // 全选店铺产品
  89. $(".shopCheck").click(function(){
  90. })
  91. // 店铺加
  92. $(".plus").click(function(){
  93. })
  94. // 店铺减
  95. $(".minus").click(function(){
  96. })
  97. // 全选
  98. $("#AllCheck").click(function(){
  99. })
  100. }
  101. /*管理ui渲染*/
  102. Shopcar.prototype.reRender = function(){
  103. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注