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

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

http://mobileshop.codeplex.com
JavaScript | 173 lines | 145 code | 28 blank | 0 comment | 12 complexity | d538dfb3cc76eb69ebda5120eac7fca0 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. window.Product = Backbone.Model.extend({
  2. defaults: {}
  3. });
  4. window.ProductList = Backbone.Collection.extend({
  5. model: Product,
  6. urlRoot: 'api/product/list/',
  7. url: function () {
  8. return this.urlRoot + localStorage.getItem('productcategoryid');
  9. }
  10. });
  11. window.ProductListView = Backbone.View.extend({
  12. el: "#jqt",
  13. initialize: function () {
  14. this.template = $('#ProductListTemplate').html();
  15. this.collection = new ProductList();
  16. },
  17. events: {
  18. 'click #ProductList .orderType ul li': 'changeOrderType',
  19. 'click #ProductList .productSampleList li': 'renderDetail',
  20. 'click #ProductList .filter a': 'renderFilter'
  21. },
  22. render: function () {
  23. var that = this;
  24. var partial = { header: $('#HeaderTemplate').html(), footer: $('#FooterTemplate').html(),
  25. innerFooter: $('#InnerFooterTemplate').html(), orderCheck: $('#ThreeCheckTemplate').html()
  26. };
  27. var data = { hasBack: true, title: "??", btnListR: [{ name: 'sort' }, { name: 'cart' }, { name: 'home'}] };
  28. this.$el.append(Mustache.render(this.template, data, partial));
  29. this.setOrderCheck();
  30. this.fetchProductList();
  31. new Plugins.SearchPlugin('#ProductList .searchContainer', {
  32. id: 'keyword_productlist'
  33. });
  34. return this;
  35. },
  36. fetchProductList: function () {
  37. var that = this;
  38. this.collection.fetch({
  39. data: {
  40. sorts: localStorage.getItem('orderType'),
  41. isASC: localStorage.getItem('isASC'),
  42. pageIndex: localStorage.getItem('pageIndex'),
  43. pageCount: 5
  44. },
  45. success: function () {
  46. var products = that.collection.models[0].get('Data'),
  47. allCount = that.collection.models[0].get('Count');
  48. if (allCount == 0) {
  49. var template = $('#EmptyInnerListTemplate').html();
  50. $('.productSampleList').html(Mustache.render(template, [], []));
  51. $('.footer').hide();
  52. $('.orderType').hide();
  53. return;
  54. }
  55. var template = $('#InnerListTemplate').html();
  56. var data = { products: products }
  57. $('#ProductList .productSampleList').html(Mustache.render(template, data, []));
  58. new Plugins.PagingPlugin('.foot-paging', {
  59. allCount: allCount,
  60. prevCall: function (e) { that.fetchPrev(e); },
  61. nextCall: function (e) { that.fetchNext(e) },
  62. numberClickCall: function (e) { that.fetchSpecific(e) }
  63. });
  64. pageView.resizeScroll();
  65. }
  66. });
  67. },
  68. fetchPrev: function (e) {
  69. if ($(e.currentTarget).hasClass('disable')) return;
  70. var pageIndex = localStorage.getItem('pageIndex');
  71. localStorage.setItem('pageIndex', +pageIndex - 1);
  72. this.fetchProductList();
  73. },
  74. fetchNext: function (e) {
  75. if ($(e.currentTarget).hasClass('disable')) return;
  76. var pageIndex = localStorage.getItem('pageIndex');
  77. localStorage.setItem('pageIndex', +pageIndex + 1);
  78. this.fetchProductList();
  79. },
  80. fetchSpecific: function (e) {
  81. var target = $(e.currentTarget);
  82. if (target.hasClass('gray')) return;
  83. var index = +target.attr('index');
  84. localStorage.setItem('pageIndex', index);
  85. this.fetchProductList();
  86. },
  87. renderDetail: function (e) {
  88. var target = $(e.currentTarget);
  89. var price = target.find('.pro-price').text(),
  90. productid = target.attr('productid');
  91. localStorage.setItem('productDetail', JSON.stringify({ productid: productid, price: price }));
  92. localStorage.setItem('productid', productid);
  93. localStorage.setItem('detailType', 'tab_intro');
  94. localStorage.setItem('commentPageIndex', 1);
  95. localStorage.setItem('ordercommentPageIndex', 1);
  96. pageView.goTo('ProductDetail');
  97. },
  98. renderFilter: function (e) {
  99. pageView.goTo('MyFilter');
  100. },
  101. setOrderCheck: function () {
  102. var orderType = localStorage.getItem('orderType'),
  103. isASC = localStorage.getItem('isASC'),
  104. priceClass = isASC == "true" ? 'up' : 'down',
  105. activeClass = 'selected',
  106. currentTarget = $('.orderType ').find('li[ordertype="' + orderType + '"]');
  107. currentTarget.addClass(activeClass).siblings().removeClass(activeClass);
  108. if (currentTarget.hasClass('price')) {
  109. currentTarget.find('a').removeClass('up down').addClass(priceClass);
  110. }
  111. },
  112. changeOrderType: function (e) {
  113. var target = $(e.currentTarget),
  114. activeClass = 'selected';
  115. if (target.hasClass(activeClass)) {
  116. this.changeAsc(e);
  117. return;
  118. }
  119. target.addClass(activeClass).siblings().removeClass(activeClass);
  120. if (!target.hasClass('price')) {
  121. $('.price').find('a').removeClass('down').addClass('up');
  122. }
  123. localStorage.setItem('orderType', target.attr('orderType'));
  124. localStorage.setItem('isASC', true);
  125. this.fetchProductList();
  126. },
  127. changeAsc: function (e) {
  128. var target = $(e.currentTarget),
  129. activeClass = 'selected';
  130. if (target.hasClass('price')) {
  131. this.changePriceImg(target);
  132. }
  133. var orderAsc = localStorage.getItem('isASC');
  134. localStorage.setItem('isASC', orderAsc == "true" ? false : true);
  135. this.fetchProductList();
  136. },
  137. changePriceImg: function (target) {
  138. var icon = $(target).find('a'),
  139. asc = icon.hasClass('up');
  140. if (asc) {
  141. icon.removeClass('up').addClass('down');
  142. } else
  143. icon.removeClass('down').addClass('up');
  144. }
  145. });