0

我怎样才能正确地重构这段代码(它可以工作,但它需要放在一个单独的函数中)当我把它放在下面的函数中时,我得到一个错误 -getConcatCategories is not a function

这是一个代码,我想在里面的同一个地方调用这个函数if

   define([
    'jquery',
    'can',
    'controls/base',
    'owl',
    'equalHeight',
    'imagesLoaded',
    'coreMods/categoryNav/categoryNav_model',
    'mustache!./relatedCategoryFeaturedMerchants_view'
  ],
  function ($, can, BaseControl, owl, equalHeight, imagesLoaded, CategoryNavModel, relatedCategoryFeaturedMerchantsView ) {

var MAX_FEATURED_MERCHANTS = 6;
var MAX_CATEGORIES = 2;

var StoreRelatedMerchantsControl = BaseControl({
  defaults: {
    moduleConfigName: 'relatedCategoryFeaturedMerchants',
    sectionHeader: '',
    hideRebatePrefix: true,
    useOldRebateValueWithoutCurrency: true,
    rebateAdditionalPrefix: 'Shop and',
    useCategoryNameForViewAllLink: false,
    viewAllLinkLanguage: 'View all',
    tiered: {
      useOldRebateValueWithoutCurrency: true,
      rebateAdditionalPrefix: 'Shop and earn',
    },
    currency: 'miles',
    showHeaderDivider: false,
    useCarousel: true,
    useConcatCategories: false,
  }
}, {
  init: function(element, options) {
    var that = this;

    var associatedCategoryIds = this.getAssociatedCategoryIds(options.merchant.categories);

    CategoryNavModel.findOne()
      .done(function (response) {
        var categories = [];

        response.each(function(category, index) {
          category.attr('featured', category.featured.slice(0, MAX_FEATURED_MERCHANTS));
          var hasFeaturedMerchants = category.featured && category.featured.length > 0;
          associatedCategoryIds.indexOf(category.id) !== -1 && hasFeaturedMerchants && categories.push(category);
        })

        var categoriesToDisplay = categories.slice(0, MAX_CATEGORIES);

        console.log(options.useConcatCategories);

        if (options.useConcatCategories) {
          //categoriesToDisplay = this.getConcatCategories(categories);
          categoriesToDisplay[0].attr('featured',
            categoriesToDisplay[0].attr('featured').concat(categoriesToDisplay[1].attr('featured')).slice(0, MAX_FEATURED_MERCHANTS)
          );
          categoriesToDisplay = categories.slice(0, 1);
          console.log(categoriesToDisplay);
        }

        if (categoriesToDisplay.length) {
          can.trigger(mn.events, 'relatedCategoryMerchants.confirmAvailability');

          var viewParams = {
            state: that.options,
            categories: categoriesToDisplay,
            mallServer: mn.mallServer,
          };

          that.element.html(can.view(relatedCategoryFeaturedMerchantsView, viewParams));

          that.$carouselHolder = that.element.find('.mn_merchantsList');
          that.manageCarouselAvailability();
        }
      })
      .fail(function() {
        mn.warn(arguments);
      });
  },

 // getConcatCategories: function(categoriesToDisplay) {
///    
  //  return categoriesToDisplay;
 // },

  getAssociatedCategoryIds: function(categories) {
    var associatedCategoryIds = [];

    categories.each(function(category) {
      associatedCategoryIds.push(category.id);
    })

    return associatedCategoryIds;
  },

  initCarousel: function () {
    if (!this.$carouselHolder.data('owl.carousel')) {
      this.$carouselHolder.addClass('owl-carousel owl-carousel-slider');
      this.$carouselHolder.addClass('mn_carousel');

        this.$merchantsList = this.element.find('.mn_dynamicMerchantsList');

        // smooth loading effect
        imagesLoaded(this.$merchantsList.find('.mn_logo')).on('progress', function (instance, image) {
            image.img.className += ' mn_loaded';
        });

        this.$carouselHolder.owlCarousel(
        $.extend(
          {
            items: 2,
            navSpeed: 800,
            margin: 20,
            dots: false,
            nav: false,
            responsive: {
              480: {
                items: 3
              },
              768: {
                items: 4,
                nav: true
              },
              950: {
                items: 6,
              }
            },
            navText: [
              '<span class="mn_carouselArrow mn_carouselArrowLeft" aria-hidden="true"></span>',
              '<span class="mn_carouselArrow mn_carouselArrowRight" aria-hidden="true"></span>'
            ],
            onInitialized: function() {
              mn.owlCarouselAccessibility();
            },
          },
          this.options.carousel
        )
      );
    }
  },

  destroyCarousel: function () {
    if (this.$carouselHolder.data('owl.carousel')) {
      this.$carouselHolder.trigger('destroy.owl.carousel');
      this.$carouselHolder.removeClass('owl-carousel owl-carousel-slider mn_carousel');
    }
  },

  '{window} resizeEnd': function(){
    this.manageCarouselAvailability();
  },

  manageCarouselAvailability: function(){
    if (this.isAllowedToDisplayCarousel()) {
      this.initCarousel();
    } else {
      this.destroyCarousel();
    }
  },

  isAllowedToDisplayCarousel: function(){
    return this.$carouselHolder && this.options.useCarousel;
  },
});

return StoreRelatedMerchantsControl;

});

所以我需要在同一个 if 语句中调用这个函数:if (options.useConcatCategories) {

但是现在在语句中的代码应该在下面命名的单独函数中 // getConcatCategories: function(categoriesToDisplay) {

但是当我这样说时 - 我收到一个错误: is not a function 我做错了什么?

4

0 回答 0