0

嗨伙计!

我目前正在开发一个客户端项目,在那里我看到自己一遍又一遍地执行相同的 javascript 代码。所以我认为将逻辑包装在自定义 jQuery 插件中会很有用。我已经为插件的单个实例实现了它,但是对于多个实例,我认为我遇到了每个实例的属性相互覆盖的问题。

好吧,让我们来看看代码!这是我为插件提供的当​​前代码:

// RESPONSIVE MENU ===========================//
// wrapper for a responsive menu plugin,      //
// made by Favolla Comunicação                //
//============================================//

/* INSTRUCTIONS
 Apply the plugin on the main wrapper of the responsive menu. For example:

 $(#menu).responsiveMenu($(#trigger));

 The plugin just toggles the classes, leaving the effects and layout for the css

 CONFIG
 - trigger: the selector of the button that will activate the menu (required)
 - activeClass: class name to be injectet when the toggle is activated (default: active)
 - submenuTrigger: the selector of the buttons that will activate the submenus, if the menu will have another levels (default: $('sub-toggle'))
 - submenu: the selector of the submenus (default: $('.submenu'))
 - submenuActiveClass: class name to be injected on the submenus when they are activated (default: open)
 - breakpoint: max window whidth where the plugin will work (default: 720)
 - timeOut: time in milissegundos to limite the onResize repeat. (default: 100)
 - moveCanvas: option to activate the "off canvas" pattern or not (just puts a class on the main elements of the page). (default: false)
 - canvas: class name of the elements that build the "canvas" (default: null)
*/

;(function ( $, window, document, undefined ) {

  $.fn.responsiveMenu = function(settings){

     var config = {
          'trigger': '',
          'activeClass': 'active',
          'submenuTrigger': $('.sub-toggle'),
          'submenu': false,
          'submenuActiveClass': 'open',
          'breakpoint': 720,
          'timeOut': 100,
          'moveCanvas': false,
          'canvas': '',
      };

      if (settings){$.extend(config, settings);}

      // plugin variables
      var mTrigger,
          menu = $(this),
          active = config.activeClass,
          button = config.trigger,
          bpoint = config.breakpoint,
          submTrigger = config.submenuTrigger,
          submenu = config.submenu,
          submenuActive = config.submenuActiveClass;
          canvasOn = config.moveCanvas;
          canvas = config.canvas;
          time = config.timeOut;

    return this.each(function () {

      if($(window).width() > bpoint){
        mTrigger = false;
      } else {
        mTrigger = true;
      }

      onChange = function(){

        clearTimeout(resizeTimer);
        var resizeTimer = setTimeout(function(){

          if($(window).width() > bpoint){
            mTrigger = false;
            menu.removeClass(active);
            button.removeClass(active);
            if(canvasOn){
              canvas.removeClass(active);
            }

          } else {
            mTrigger = true;
          }
        }, time);

      }

      $(window).bind('resize',onChange);
      $(document).ready(onChange);

      button.click(function(){
        if(mTrigger) {
          menu.toggleClass(active);
          button.toggleClass(active);
          if(canvasOn){
            canvas.toggleClass(active);
          }
        }
      });

      if(submenu){

        var submenuClass = '.' + submenu.prop('class');

        // toggle for the submenus
        submTrigger.click(function(){

          if(mTrigger) {

            if($(this).hasClass(active)){

              submTrigger.removeClass(active);
              submenu.removeClass(submenuActive);

            } else {
              submTrigger.removeClass(active);
              $(this).addClass(active);
              submenu.removeClass(submenuActive);
              $(this).next(submenuClass).addClass(submenuActive);
            }

          }

        }); 
      }

    });
  }
})( jQuery, window, document );

然后,当我想应用插件时,我会这样:

  $('#menu-wrapper').responsiveMenu({
    trigger: $('#nav-toggle'),
    submenu: $('.submenu'),
    submenuTrigger: $('.submenu-toggle'),
    moveCanvas: true,
    canvas: $('.canvas'),
    breakpoint: 862
  });

  $('#search').responsiveMenu({
    trigger: $('#search-toggle'),
    breakpoint: 862
  });

这里的主要问题是当我设置 的实例时responsiveMenu();,似乎某些选项被覆盖了。例如,第一个实例设置moveCanvas为 true,它可以工作,但是当我将第二个实例留空时(这使得该元素的moveCanvas选项设置false为,第一个实例的这个选项不再起作用。

我知道也许我没有遵循 jQuery 插件的最佳实践,我什至读过一些关于jQuery Boilerplate的东西,看起来很棒,但我不是高级 javascript 开发人员,所以有很多事情我可以做得更好,但我现在不知道该怎么办。

无论如何,非常欢迎有关此问题的任何帮助(以及有关插件的意见)!

4

2 回答 2

0
var mTrigger,
    menu = $(this),
    active = config.activeClass,
    button = config.trigger,
    bpoint = config.breakpoint,
    submTrigger = config.submenuTrigger,
    submenu = config.submenu,

直到这里你做对了。

    submenuActive = config.submenuActiveClass;
    canvasOn = config.moveCanvas;
    canvas = config.canvas;
    time = config.timeOut;

然后,您引入了一个分号 - 这导致进一步的赋值 ( canvasOn,canvastime) 不再是var语句的一部分。它们不是变量声明,您在这里分配给全局变量 - 这样您就可以覆盖第一个插件实例的设置。

将除最后一个分号之外的所有分号都更改为逗号。

于 2013-08-02T16:51:31.687 回答
0

您需要将您的设置封装在一个类或闭包中,并为调用插件的每个元素存储它们。

return this.each(function () {

    ...

    $(this).data('some-key', settings);

    ...

});

如果你想了解 jQuery 插件创作,他们在这里有一篇文章http://learn.jquery.com/plugins/basic-plugin-creation/

于 2013-08-02T16:49:02.503 回答