0

I have this Drupal behaviors function:

(function ($) {




    Drupal.behaviors.modulenameSwiper = {
        attach : function (context, settings) {
        if ( location.pathname != '/en/imce' ||              location.pathname != '/fr/imce' ||              location.pathname != '/it/imce'     ) {

        var mySwiper = new Array();
        var mySwiperIndex = 0;

        $('.swiper-container').each(function (){
          mySwiper[mySwiperIndex] = new Swiper ($(this)[0],{
              mode:'horizontal',
              loop: false, 
              slidesPerView: 'auto'
              });
          });

          mySwiperIndex++;

    }
    }
  }
 }(jQuery));

I need to call mySwiper object out of the Behaviors callback. there is anyway to make it?

1

You could assign mySwiper to a property within the behavior.

(function ($) {
  Drupal.behaviors.modulenameSwiper = {
    attach : function (context, settings) {
      if ( location.pathname != '/en/imce' || location.pathname != '/fr/imce' ||              location.pathname != '/it/imce'     ) {
        mySwiper = this.get();
        $('.swiper-container').each(function (){
          mySwiper.instances[mySwiper.index] = new Swiper ($(this)[0],{
            mode:'horizontal',
            loop: false, 
            slidesPerView: 'auto'
          });
        });
      }
    },
    /**
     * Returns an object with Swiper instances and highest index.
     */
    getSwipper : function () {
      if (this.mySwiper === 'undefined') {
        this.mySwiper.instances = new Array();
        this.mySwiper.index = 0;
      } else {
        this.mySwiper.index++;
      }
      return this.mySwiper;
    },
  };

  $(document).ready(function () {
    console.log(Drupal.behaviors.modulenameSwiper.getSwiper());
  });
}(jQuery));
5
  • Hello Juampy, Thanks, but the problem is that I need to make a callback out of the Drupal.behaviors.modulenameSwiper . So If I try to call it, it doesn't work. It told my that it's not defined. Jan 3 '14 at 9:58
  • @OskarCalvo Surely you mean Drupal.behaviors.modulenameSwiper.attach()? By "make a callback" I'm assuming you mean "call/invoke the function"?
    – Clive
    Jan 3 '14 at 9:59
  • Hello Clive. Drupal.behaviors.modulenameSwiper.attach() give me access to the function. What I need is to load every mySwiper object and rebuild it. Jan 3 '14 at 10:06
  • Ah ok, this isn't really related to Drupal in any way - you're just trying to gain access to variables outside your current scope, purely a javascript thing. I would go with Juampy's solution, and use Drupal.attachBehaviors() afterwards
    – Clive
    Jan 3 '14 at 10:13
  • Juampy, yours code Works, I don't see the line "Drupal.behaviors.modulenameSwiper.mySwiper = mySwiper;", and it's the trick. Thanks Juampy and Clive. Jan 3 '14 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.