Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. /* global
  2. define: false,
  3. Backbone: false
  4. */
  5.  
  6. 'use strict';
  7.  
  8. define([
  9. 'marionette',
  10. 'hbs!special-Football/templates/layout',
  11. 'special-Football/views/header',
  12. 'special-Football/views/items'
  13. ], function(Marionette, Template, HeaderView, ItemsView) {
  14.  
  15. var DEFAULT_LIMIT = 10,
  16. SHOW_ALL = 'All';
  17.  
  18. return Backbone.Marionette.Layout.extend({
  19. el: '.special-Football',
  20.  
  21. template: Template,
  22.  
  23. regions: {
  24. header: '.special-Football__selection',
  25. items: '.special-Football__items-container'
  26. },
  27.  
  28. ui: {
  29. noResults: '.special-Football__no-results',
  30. viewMoreToggle: '.section-toggle'
  31. },
  32.  
  33. events: {
  34. 'click @ui.viewMoreToggle': 'toggleViewMore'
  35. },
  36.  
  37. limit: DEFAULT_LIMIT,
  38. currentFootballKeys: SHOW_ALL,
  39. offerCategories: { title: SHOW_ALL },
  40.  
  41. initialize: function(options) {
  42. _.bindAll(this, 'setCurrentFootball', 'toggleViewMore');
  43.  
  44. this.Football = options.FootballData;
  45.  
  46. if (!this.Football || !_.keys(this.Football).length) {
  47. return;
  48. }
  49.  
  50. this.setupFootball();
  51. },
  52.  
  53. setupFootball: function() {
  54. this.offerCategories = _.map(_.keys(this.Football), function(offer) {
  55. return {
  56. title: offer
  57. };
  58. });
  59.  
  60. this.offerCategories.unshift({ title: SHOW_ALL });
  61.  
  62. this.setCurrentFootball();
  63. },
  64.  
  65. setCurrentFootball: function(FootballKey) {
  66. var Football;
  67.  
  68. if (!this.currentFootball) {
  69. this.currentFootball = new Backbone.Collection;
  70. }
  71.  
  72. if (!FootballKey || FootballKey === SHOW_ALL) {
  73. Football = _.chain(this.Football).values().flatten().value();
  74. } else {
  75. Football = this.Football[FootballKey];
  76. }
  77.  
  78. Football = Football.slice(0, this.limit === SHOW_ALL ? undefined : this.limit);
  79.  
  80. this.currentFootballKey = FootballKey || SHOW_ALL;
  81. this.currentFootball.reset(Football);
  82.  
  83. this.updateToggleViewMore();
  84. this.updateNoResults();
  85. },
  86.  
  87. onRender: function() {
  88. this.header.show(new HeaderView({
  89. categories: this.offerCategories,
  90. setCurrentFootball: this.setCurrentFootball
  91. }));
  92.  
  93. this.items.show(new ItemsView({
  94. collection: this.currentFootball
  95. }));
  96.  
  97. this.updateToggleViewMore();
  98. this.updateNoResults();
  99. },
  100.  
  101. toggleViewMore: function(e) {
  102. e.preventDefault();
  103.  
  104. this.limit = (this.limit === SHOW_ALL) ? DEFAULT_LIMIT : SHOW_ALL;
  105.  
  106. this.setCurrentFootball(this.currentFootballKey);
  107. },
  108.  
  109. updateToggleViewMore: function() {
  110. var currentFootball = this.Football[this.currentFootballKey],
  111. FootballTotal = currentFootball ? currentFootball.length : _.chain(this.Football).values().flatten().value().length;
  112.  
  113. $(this.ui.viewMoreToggle).toggle(FootballTotal > DEFAULT_LIMIT);
  114. },
  115.  
  116. updateNoResults: function() {
  117. $(this.ui.noResults).toggle(!this.currentFootball || this.currentFootball.length === 0);
  118. }
  119. });
  120. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement