Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* global
- define: false,
- Backbone: false
- */
- 'use strict';
- define([
- 'marionette',
- 'hbs!special-Football/templates/layout',
- 'special-Football/views/header',
- 'special-Football/views/items'
- ], function(Marionette, Template, HeaderView, ItemsView) {
- var DEFAULT_LIMIT = 10,
- SHOW_ALL = 'All';
- return Backbone.Marionette.Layout.extend({
- el: '.special-Football',
- template: Template,
- regions: {
- header: '.special-Football__selection',
- items: '.special-Football__items-container'
- },
- ui: {
- noResults: '.special-Football__no-results',
- viewMoreToggle: '.section-toggle'
- },
- events: {
- 'click @ui.viewMoreToggle': 'toggleViewMore'
- },
- limit: DEFAULT_LIMIT,
- currentFootballKeys: SHOW_ALL,
- offerCategories: { title: SHOW_ALL },
- initialize: function(options) {
- _.bindAll(this, 'setCurrentFootball', 'toggleViewMore');
- this.Football = options.FootballData;
- if (!this.Football || !_.keys(this.Football).length) {
- return;
- }
- this.setupFootball();
- },
- setupFootball: function() {
- this.offerCategories = _.map(_.keys(this.Football), function(offer) {
- return {
- title: offer
- };
- });
- this.offerCategories.unshift({ title: SHOW_ALL });
- this.setCurrentFootball();
- },
- setCurrentFootball: function(FootballKey) {
- var Football;
- if (!this.currentFootball) {
- this.currentFootball = new Backbone.Collection;
- }
- if (!FootballKey || FootballKey === SHOW_ALL) {
- Football = _.chain(this.Football).values().flatten().value();
- } else {
- Football = this.Football[FootballKey];
- }
- Football = Football.slice(0, this.limit === SHOW_ALL ? undefined : this.limit);
- this.currentFootballKey = FootballKey || SHOW_ALL;
- this.currentFootball.reset(Football);
- this.updateToggleViewMore();
- this.updateNoResults();
- },
- onRender: function() {
- this.header.show(new HeaderView({
- categories: this.offerCategories,
- setCurrentFootball: this.setCurrentFootball
- }));
- this.items.show(new ItemsView({
- collection: this.currentFootball
- }));
- this.updateToggleViewMore();
- this.updateNoResults();
- },
- toggleViewMore: function(e) {
- e.preventDefault();
- this.limit = (this.limit === SHOW_ALL) ? DEFAULT_LIMIT : SHOW_ALL;
- this.setCurrentFootball(this.currentFootballKey);
- },
- updateToggleViewMore: function() {
- var currentFootball = this.Football[this.currentFootballKey],
- FootballTotal = currentFootball ? currentFootball.length : _.chain(this.Football).values().flatten().value().length;
- $(this.ui.viewMoreToggle).toggle(FootballTotal > DEFAULT_LIMIT);
- },
- updateNoResults: function() {
- $(this.ui.noResults).toggle(!this.currentFootball || this.currentFootball.length === 0);
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement