PageRenderTime 79ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://mobileshop.codeplex.com
JavaScript | 324 lines | 284 code | 40 blank | 0 comment | 22 complexity | cbb14988eb5f864dd13ceb45dc8c13ab MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. window.ProductDetail = Backbone.Model.extend({
  2. urlRoot: 'api/product/',
  3. url: function () {
  4. return this.urlRoot + localStorage.getItem('productid');
  5. },
  6. defaults: {
  7. BaskOrderLogCount: 0
  8. },
  9. parse: function (model) {
  10. model['BaskOrderLogCount'] = model['BaskOrderLogCount'] || this.defaults.BaskOrderLogCount;
  11. for (i in model['Images']) {
  12. model['Images'][i] = { src: model['Images'][i] };
  13. };
  14. model['hasImages'] = model['Images'].length > 0;
  15. return model;
  16. }
  17. });
  18. window.Comment = Backbone.Model.extend({});
  19. window.CommentList = Backbone.Collection.extend({
  20. model: Comment,
  21. urlRoot: 'api/product/comment/',
  22. url: function () {
  23. return this.urlRoot + localStorage.getItem('productid');
  24. }
  25. });
  26. window.OrderComment = Backbone.Model.extend({});
  27. window.OrderCommentDetail = Backbone.Model.extend({
  28. urlRoot: 'api/product/baskorderlogdetail/',
  29. url: function () {
  30. return this.urlRoot + localStorage.getItem('ordercommentid');
  31. }
  32. });
  33. window.OrderCommentList = Backbone.Collection.extend({
  34. model: OrderComment,
  35. urlRoot: 'api/product/baskorderlog/',
  36. url: function () {
  37. return this.urlRoot + localStorage.getItem('productid');
  38. }
  39. });
  40. window.ProductDetailView = Backbone.View.extend({
  41. el: '#jqt',
  42. initialize: function () {
  43. this.template = $('#ProductDetailTemplate').html();
  44. this.model = new ProductDetail();
  45. this.commentList = new CommentList();
  46. this.orderCommentList = new OrderCommentList();
  47. this.orderCommentDetail = new OrderCommentDetail();
  48. },
  49. events: {
  50. 'click #ProductDetail .sliding li': 'fetchSpecific',
  51. 'click #ProductDetail .p_intro_info .ul-detail,.p_summary .arrow-prdetail': 'fetchDetail',
  52. 'click #ProductDetail .p_intro_info .ul-comment': 'fetchComment',
  53. 'click #ProductDetail .p_intro_info .ul-orderComment': 'fetchOrderComment',
  54. 'click #ProductDetail #p_ordercomment li': 'fetchOrderCommentDetail',
  55. 'click #add_cart': 'addToMyCart',
  56. 'click #addFavorite': 'addToMyCollect'
  57. },
  58. render: function () {
  59. var partial = { header: $('#HeaderTemplate').html(), footer: $('#FooterTemplate').html(),
  60. innerFooter: $('#InnerFooterTemplate').html(), sliding: $('#SlideTitleTemplate').html(),
  61. pictureList: $('#PictureSlideTemplate').html()
  62. };
  63. var data = { hasBack: true, title: "??", btnListR: [{ name: 'sort' }, { name: 'cart' }, { name: 'home'}] };
  64. this.$el.append(Mustache.render(this.template, data, partial));
  65. this.setFetchType();
  66. this.fetchSpecific();
  67. return this;
  68. },
  69. fetchSpecific: function (e) {
  70. if (e) {
  71. var target = $(e.currentTarget);
  72. localStorage.setItem('detailType', target.attr('detail'));
  73. this.setFetchType();
  74. }
  75. var type = localStorage.getItem('detailType');
  76. switch (type) {
  77. case 'tab_intro': this.fetchIntro(); break;
  78. case 'tab_detail': this.fetchDetail(); break;
  79. case 'tab_comment': this.fetchComment(); break;
  80. case 'tab_orderComment': this.fetchOrderComment(); break;
  81. default: this.fetchIntro(); break;
  82. }
  83. },
  84. fetchIntro: function () {
  85. var that = this,
  86. type = 'tab_intro';
  87. localStorage.setItem('detailType', type); this.setFetchType();
  88. $('#p_intro').show().siblings().hide();
  89. this.model.fetch({
  90. success: function () {
  91. var template = $('#IntroContentTemplate').html();
  92. var partial = { pictureSlide: $('#PictureSlideTemplate').html(), productSummary: $('#SummaryTemplate').html(),
  93. commentSample: $('#SampleCommentTemplate').html()
  94. }
  95. $('#p_intro').html(Mustache.render(template, that.model.toJSON(), partial));
  96. $('.flexslider').flexslider({
  97. animation: "slide",
  98. slideshow: false
  99. });
  100. if (that.model.get('RepertoryCount') == 0) {
  101. $('#add_cart').hide();
  102. }
  103. pageView.resizeScroll();
  104. $('li#Specifications').html(Mustache.render($('#DetailContentTemplate').html(),
  105. { props: that.model.get('ProductProperty') || {} }, []));
  106. $('#p_detail').html(Mustache.render($('#DetailContentTemplate').html(), that.model.toJSON(), []));
  107. }
  108. });
  109. },
  110. fetchDetail: function () {
  111. var that = this,
  112. type = 'tab_detail';
  113. localStorage.setItem('detailType', type); this.setFetchType();
  114. $('#p_detail').show().siblings().hide();
  115. this.model.fetch({
  116. success: function () {
  117. var template = $('#DetailContentTemplate').html();
  118. var propTemplate = $('#SpecificationsTemplate').html(),
  119. propData = { props: that.model.get('ProductProperty') || {} }
  120. $('li#Specifications').html(Mustache.render(propTemplate, propData, []));
  121. $('#p_detail').html(Mustache.render(template, that.model.toJSON(), []));
  122. pageView.resizeScroll();
  123. $('#p_intro').html(Mustache.render($('#IntroContentTemplate').html(), that.model.toJSON(),
  124. { pictureSlide: $('#PictureSlideTemplate').html(), productSummary: $('#SummaryTemplate').html(),
  125. commentSample: $('#SampleCommentTemplate').html()
  126. }));
  127. }
  128. });
  129. },
  130. fetchComment: function () {
  131. var that = this,
  132. type = 'tab_comment';
  133. localStorage.setItem('detailType', type); this.setFetchType();
  134. $("#p_comment").show().siblings().hide();
  135. this.commentList.fetch({
  136. data: {
  137. startPage: +localStorage.getItem('commentPageIndex'),
  138. pageSize: 5
  139. },
  140. success: function () {
  141. var commentList = that.commentList.models[0].get('Data'),
  142. allCount = that.commentList.models[0].get('Count');
  143. if (allCount == 0) {
  144. var template = $('#EmptyCommentContentTemplate').html();
  145. $('#p_comment').html(Mustache.render(template, [], []));
  146. $('.footer').hide();
  147. return;
  148. }
  149. var template = $('#CommentContentTemplate').html();
  150. var data = { commentList: commentList };
  151. var partial = { commentSample: $('#DetailCommentTemplate').html() };
  152. $('#p_comment').html(Mustache.render(template, data, partial));
  153. new Plugins.PagingPlugin('.foot-paging', {
  154. allCount: allCount,
  155. pageIndexKey: 'commentPageIndex',
  156. prevCall: function (e) { that.fetchPrevComment(e); },
  157. nextCall: function (e) { that.fetchNextComment(e) },
  158. numberClickCall: function (e) { that.fetchSpecificComment(e) }
  159. });
  160. pageView.resizeScroll();
  161. }
  162. });
  163. },
  164. fetchPrevComment: function (e) {
  165. if ($(e.currentTarget).hasClass('disable')) return;
  166. var pageIndex = localStorage.getItem('commentPageIndex');
  167. localStorage.setItem('commentPageIndex', +pageIndex - 1);
  168. this.fetchComment();
  169. },
  170. fetchNextComment: function (e) {
  171. if ($(e.currentTarget).hasClass('disable')) return;
  172. var pageIndex = localStorage.getItem('commentPageIndex');
  173. localStorage.setItem('commentPageIndex', +pageIndex + 1);
  174. this.fetchComment();
  175. },
  176. fetchSpecificComment: function (e) {
  177. var target = $(e.currentTarget);
  178. if (target.hasClass('gray')) return;
  179. var index = +target.attr('index');
  180. localStorage.setItem('commentPageIndex', index);
  181. this.fetchComment();
  182. },
  183. fetchOrderComment: function () {
  184. var that = this,
  185. type = 'tab_orderComment';
  186. localStorage.setItem('detailType', type); this.setFetchType();
  187. $('#p_ordercomment').show().siblings().hide();
  188. this.orderCommentList.fetch({
  189. data: {
  190. startPage: +localStorage.getItem('ordercommentPageIndex'),
  191. pageSize: 5
  192. },
  193. success: function () {
  194. var orderComments = that.orderCommentList.models[0].get('Data'),
  195. allCount = that.orderCommentList.models[0].get('Count');
  196. if (allCount == 0) {
  197. var template = $('#EmptyOrderCommentContentTemplate').html();
  198. $('#p_ordercomment').html(Mustache.render(template, [], []));
  199. $('.footer').hide();
  200. return;
  201. }
  202. var template = $('#OrderCommentContentTemplate').html();
  203. var data = { orderComments: orderComments };
  204. $('#p_ordercomment').html(Mustache.render(template, data, []));
  205. new Plugins.PagingPlugin('.foot-paging', {
  206. allCount: allCount,
  207. pageIndexKey: 'ordercommentPageIndex',
  208. prevCall: function (e) { that.fetchPrevOrderComment(e); },
  209. nextCall: function (e) { that.fetchNextOrderComment(e) },
  210. numberClickCall: function (e) { that.fetchSpecificOrderComment(e) }
  211. });
  212. pageView.resizeScroll();
  213. }
  214. });
  215. },
  216. fetchPrevOrderComment: function (e) {
  217. if ($(e.currentTarget).hasClass('disable')) return;
  218. var pageIndex = localStorage.getItem('ordercommentPageIndex');
  219. localStorage.setItem('ordercommentPageIndex', +pageIndex - 1);
  220. this.fetchOrderComment();
  221. },
  222. fetchNextOrderComment: function (e) {
  223. if ($(e.currentTarget).hasClass('disable')) return;
  224. var pageIndex = localStorage.getItem('ordercommentPageIndex');
  225. localStorage.setItem('ordercommentPageIndex', +pageIndex + 1);
  226. this.fetchOrderComment();
  227. },
  228. fetchSpecificOrderComment: function (e) {
  229. var target = $(e.currentTarget);
  230. if (target.hasClass('gray')) return;
  231. var index = +target.attr('index');
  232. localStorage.setItem('ordercommentPageIndex', index);
  233. this.fetchOrderComment();
  234. },
  235. fetchOrderCommentDetail: function (e) {
  236. var target = $(e.currentTarget),
  237. that = this;
  238. localStorage.setItem('ordercommentid', target.attr('orderid'));
  239. this.orderCommentDetail.fetch({
  240. success: function () {
  241. var template = $('#OrderCommentDetailTemplate').html();
  242. var data = { orderDetail: that.orderCommentDetail.get('BaskOrderLog'), comments: that.orderCommentDetail.get('BaskOrderComments') };
  243. var partial = { detail: $('#OrderCommentContentDetailTemplate').html(),
  244. commentList: $('#OrderCommentListDetailTemplate').html(),
  245. pictureSlide: $('#PictureSlideTemplate').html()
  246. };
  247. $('#p_ordercomment').html(Mustache.render(template, data, partial));
  248. $('.flexslider').flexslider({
  249. animation: "slide",
  250. slideshow: false
  251. });
  252. pageView.resizeScroll();
  253. }
  254. });
  255. },
  256. setFetchType: function () {
  257. var type = localStorage.getItem('detailType'),
  258. activeClass = 'selected',
  259. target = $('#ProductDetail .sliding').find('li[detail="' + type + '"]');
  260. $(target).addClass(activeClass).siblings().removeClass(activeClass);
  261. pageView.resizeScroll();
  262. },
  263. addToMyCart: function () {
  264. if (pageView.getCookieValue('uid')) {
  265. var product = JSON.parse(localStorage.getItem('productDetail'));
  266. MyCartView.addToCarts(product['productid'], product['price'], 1, function () {
  267. pageView.goTo('MyCart');
  268. });
  269. } else {
  270. localStorage.setItem('loginfrom', 'addCart');
  271. pageView.goTo('Login');
  272. }
  273. },
  274. addToMyCollect: function () {
  275. if (pageView.getCookieValue('uid')) {
  276. MyCollectView.addToMyCollect(function () {
  277. pageView.goTo('MyCollect');
  278. });
  279. } else {
  280. localStorage.setItem('loginfrom', 'addCollect');
  281. pageView.goTo('Login');
  282. }
  283. }
  284. });