PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://mobileshop.codeplex.com
JavaScript | 285 lines | 238 code | 46 blank | 1 comment | 39 complexity | 5e1c508a160487e0beab8f18995db7c4 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. window.ErrorPageView = Backbone.View.extend({
  2. el: '#jqt',
  3. initialize: function () {
  4. this.template = $('#ErrorPageTemplate').html();
  5. },
  6. render: function () {
  7. this.$el.append(Mustache.render(this.template, [], []));
  8. return this;
  9. }
  10. });
  11. var route = [
  12. { pageId: "Login", viewClass: LoginView }
  13. , { pageId: "Register", viewClass: RegisterView }
  14. , { pageId: 'Agreement',viewClass:AgreementView }
  15. , { pageId: "Catalog", viewClass: CatalogView }
  16. , { pageId: "FirstCategory", viewClass: FirstLevelView }
  17. , { pageId: 'SecondCategory', viewClass: SecondLevelView }
  18. , { pageId: 'ThirdCategory', viewClass: ThirdLevelView }
  19. , { pageId: "ProductList", viewClass: ProductListView }
  20. , { pageId: 'ProductDetail', viewClass: ProductDetailView }
  21. , { pageId: 'MyCart', viewClass: MyCartView }
  22. , { pageId: 'MyUserInfo', viewClass: UserInfoView }
  23. , { pageId: 'OrderMake', viewClass: MakeOrderView }
  24. , { pageId: 'MyCollect', viewClass: MyCollectView }
  25. , { pageId: 'MyOrder', viewClass: MyOrderListView }
  26. , { pageId: 'SearchList', viewClass: SearchListView }
  27. , { pageId: 'MyFilter', viewClass: MyFilterView }
  28. , { pageId: 'MyMessage', viewClass: MessageView }
  29. , { pageId: 'AddressList', viewClass: AddressListView }
  30. , { pageId: 'PaymentList', viewClass: PaymentView }
  31. , { pageId: 'DispatchModeList', viewClass: DispatchView }
  32. , { pageId: 'MyInvoice', viewClass: InvoiceView }
  33. , { pageId: 'CouponList', viewClass: CouponView }
  34. , { pageId: 'GenerateOrder', viewClass: OrderGenerateView }
  35. , { pageId: 'OrderDetail', viewClass: OrderDetailView }
  36. , { pageId: 'PayDetail', viewClass: PayDetailView }
  37. , { pageId: 'ErrorPage', viewClass: ErrorPageView }
  38. ];
  39. window.Page = Backbone.Model.extend({
  40. defaults: {
  41. pageId: "Login",
  42. viewClass: LoginView
  43. }
  44. });
  45. window.PageCollection = Backbone.Collection.extend({
  46. model: Page
  47. });
  48. window.PageView = Backbone.View.extend({
  49. el: "#jqt",
  50. model: Page,
  51. events: {
  52. 'click .registerLink,.foot_register': 'renderRegister',
  53. 'click .foot_login,.link-btn[action="avatar"],.list li[action="myhome"],.foot_user': 'renderMyInfo',
  54. 'click .link-btn[action="home"]': 'renderDefaultPage',
  55. 'click .list li[action="categorys"],.link-btn[action="sort"]': 'renderFirstCategory',
  56. 'click .list li[action="collect"]': 'renderMyCollect',
  57. 'click .link-btn[action="cart"],.list li[action="buyer"]': 'renderMyCart',
  58. 'click .list li[action="message"]': 'renderMyMessage',
  59. 'click .list li[action="order"]': 'renderMyOrder',
  60. 'click .foot_exit': 'logout',
  61. 'click .header .back': 'goBack',
  62. 'click .footer .to-top': 'scrollToTop'
  63. },
  64. render: function () {
  65. pageRoute.navigate('page/' + this.model.get("pageId"));
  66. return this;
  67. },
  68. renderContent: function (viewType) {
  69. var pageId = this.model.get('pageId');
  70. if (this.$el.find('#' + pageId).length > 0) {
  71. $('#' + pageId).remove();
  72. }
  73. if (viewType && typeof viewType === "function") {
  74. var view = new viewType({ id: pageId + '12' });
  75. view.render();
  76. } //append div into jqt
  77. this.render(); //register route
  78. },
  79. renderDefaultPage: function () {
  80. this.goTo('Catalog');
  81. },
  82. renderMyInfo: function () {
  83. if (this.getCookieValue('uid'))
  84. this.goTo('MyUserInfo');
  85. else this.goTo('Login');
  86. },
  87. renderRegister: function () {
  88. this.goTo('Register');
  89. },
  90. renderFirstCategory: function () {
  91. this.goTo('FirstCategory');
  92. },
  93. renderMyCart: function () {
  94. if (this.getCookieValue('uid'))
  95. this.goTo('MyCart');
  96. else {
  97. this.goTo('Login');
  98. localStorage.setItem('loginfrom', 'fetchCart');
  99. }
  100. },
  101. renderMyCollect: function () {
  102. if (this.getCookieValue('uid'))
  103. this.goTo('MyCollect');
  104. else {
  105. this.goTo('Login');
  106. localStorage.setItem('loginfrom', 'myCollect');
  107. }
  108. },
  109. renderMyMessage: function () {
  110. localStorage.setItem('messagePageIndex', 1);
  111. if (this.getCookieValue('uid'))
  112. this.goTo('MyMessage');
  113. else {
  114. this.goTo('Login');
  115. localStorage.setItem('loginfrom', 'myMessage');
  116. }
  117. },
  118. renderMyOrder: function () {
  119. if (this.getCookieValue('uid'))
  120. this.goTo('MyOrder');
  121. else {
  122. this.goTo('Login');
  123. localStorage.setItem('loginfrom', 'myOrder');
  124. }
  125. },
  126. goTo: function (pageId) {
  127. var p = _.find(pageList.toArray(), function (model) {
  128. return model.get('pageId') === pageId;
  129. });
  130. page.set(p.toJSON());
  131. var viewType = page.get('viewClass');
  132. this.renderContent(viewType);
  133. var fromPage = this.$el.find('.current');
  134. var toPage = $('#' + pageId);
  135. var animation = { name: 'slideleft', selector: '.slideleft, .slide, #jqt > * > ul li a' };
  136. if (fromPage.length !== 0) {
  137. jQT.doNavigation(fromPage, toPage, animation);
  138. }
  139. toPage.addClass('current');
  140. this.changeFoot();
  141. this.changeCartIcon();
  142. this.registerScroll();
  143. },
  144. goBack: function () {
  145. window.history.back();
  146. },
  147. logout: function () {
  148. localStorage.clear();
  149. var username = this.getCookieValue('uid');
  150. localStorage.setItem('username', username);
  151. $.post('api/account/LogOut', { id: this.getCookieValue('uid') }, function () {
  152. pageView.goTo('Catalog');
  153. }, 'json');
  154. },
  155. scrollToTop: function () {
  156. if (this.myScroll && this.myScroll instanceof iScroll)
  157. this.myScroll.scrollTo(0, 0);
  158. },
  159. myScroll: {},
  160. registerScroll: function () {
  161. var that = this;
  162. if (that.myScroll && that.myScroll instanceof iScroll) {
  163. that.myScroll.destroy();
  164. that.myScroll = null;
  165. }
  166. that.myScroll = new iScroll(that.model.get("pageId") + "Wrapper", {
  167. onBeforeScrollStart: function (e) {
  168. var target = e.target;
  169. while (target.nodeType != 1) target = target.parentNode;
  170. var targetName = target.tagName.toLowerCase();
  171. if (targetName != 'select' && targetName != 'input' && targetName != 'textarea')
  172. e.preventDefault();
  173. }, onScrollStart: function () {
  174. $(':focus').blur();
  175. }
  176. });
  177. that.myScroll._resize();
  178. },
  179. resizeScroll: function () {
  180. var that = this;
  181. window.setTimeout(function () {
  182. if (that.myScroll && that.myScroll instanceof iScroll) {
  183. that.myScroll._resize();
  184. }
  185. }, 300);
  186. },
  187. changeFoot: function () {
  188. var loginid = this.getCookieValue('uid');
  189. if (loginid) {
  190. $('.footer').find('.foot_1').removeClass('foot_login').addClass('foot_user').text(loginid);
  191. $('.footer').find('.foot_2').removeClass('foot_register').addClass('foot_exit').text('??');
  192. } else {
  193. $('.footer').find('.foot_1').removeClass('foot_user').addClass('foot_login').text('??');
  194. $('.footer').find('.foot_2').removeClass('foot_exit').addClass('foot_register').text('??');
  195. }
  196. },
  197. changeCartIcon: function () {
  198. var hasCarts = localStorage.getItem('hasCarts'),
  199. icon = hasCarts == 'true' ? 'cartm' : 'cart';
  200. $('.header a[action="cart"]>img').attr('src', '/Content/Images/header/' + icon + '.png');
  201. },
  202. getCookieValue: function (key) {
  203. var cookie = document.cookie;
  204. if (navigator.cookieEnabled && cookie) {
  205. if (cookie.length == 0) return undefined;
  206. var value = _.find(cookie.split(';'), function (cook) {
  207. return cook.trim().split('=')[0] == key;
  208. });
  209. return value && value.trim().split('=')[1];
  210. } else return undefined;
  211. },
  212. cookieTimeoutAutoLogin: function () {
  213. if (this.getCookieValue('uid')) {
  214. var user = new AutoLoginModel({
  215. id: 1,
  216. loginid: this.getCookieValue('uid')
  217. });
  218. user.save('', '', {
  219. success: function (res) {
  220. }
  221. });
  222. }
  223. }
  224. });
  225. window.PageRoute = Backbone.Router.extend({
  226. routes: {
  227. "page/:pageId": "loadPage",
  228. "": "home"
  229. },
  230. home: function () {
  231. //render login content
  232. pageView.cookieTimeoutAutoLogin();
  233. pageView.renderDefaultPage();
  234. },
  235. loadPage: function (pageId) {
  236. pageView.goTo(pageId);
  237. }
  238. });
  239. var pageList = new PageCollection(route);