简体   繁体   English

如何将JQuery设置为对象文字模块?

[英]How would I set up JQuery into an Object Literal module?

I'm working on understanding the Object Literal design pattern in JS with JQuery. 我正在使用JQuery理解JS中的对象文字设计模式。 I have the code separated into different modules to lower the number of time the DOM has to be searched and be extendable. 我将代码分为不同的模块,以减少DOM必须搜索和可扩展的时间。 The problem is i don't know how to get the code to actually work. 问题是我不知道如何使代码真正起作用。 I understand why it's separated such as caching the Dom, binding events, and rendering the action of the slideToggle but the trouble is I don't fully understand how to apply the function to the event correctly. 我知道为什么要分开,例如缓存Dom,绑定事件和渲染slideToggle的动作,但是麻烦是我不完全了解如何正确地将函数应用于事件。

The code I'm transferring from JQuery is as follows: 我从JQuery传输的代码如下:

$(document).ready(function() {
  $('#menu-button').click(
    function(){
        $('#menu').slideToggle(400);
    }
  );
  $('.dropdown').click(
    function(){
        $('.dropdown-content').slideToggle(500);
    }
  );
});

The Object Literal Module I'm transferring this to is as follows: 我将其传送到的对象文字模块如下:

(function() {
  var menu = {
    init: function() {
        this.cacheDom();
        this.bindEvents();
        this.action();
    },
    cacheDom: function() {
        this.$el = $('nav');
        this.$menu = this.$el.find('#menu');
        this.$menuButton = this.$el.find('#menu-button');
        this.$dropdown = this.$el.find('.dropdown');
        this.$dropdownContent = this.$el.find('.dropdown-content');
    },
    bindEvents: function() {
        this.$menuButton.on('click', this.slideToggle.bind(this));
        this.$dropdownContent.on('click', this.slideToggle.bind(this));
    },
    action: function() {
        this.slideToggle();
    },
  }
  menu.init();
})()

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

In bindEvents method, this refers to menu object which does not have slideToggle method. bindEvents方法中, this是指没有slideToggle方法的menu对象。

 (function() { var menu = { init: function() { this.cacheDom(); this.bindEvents(); //this.action(); }, cacheDom: function() { this.$el = $('nav'); this.$menu = this.$el.find('#menu'); this.$menuButton = this.$el.find('#menu-button'); this.$dropdown = this.$el.find('.dropdown'); this.$dropdownContent = this.$el.find('.dropdown-content'); }, bindEvents: function() { this.$menuButton.on('click', function() { this.$menu.slideToggle() }.bind(this)); this.$dropdownContent.on('click', function() { this.$dropdownContent.slideToggle() }.bind(this)); }, action: function() { this.slideToggle(); }, } menu.init(); })() 
 #menu-button { display: none; } @media screen and (max-width: 644px) { #menu { display: none; } nav[role="full-horizontal"] { float: none; margin-bottom: 3rem; text-align: center; } nav[role="full-horizontal"] ul { width: 100%; margin-top: 3rem; display: block; padding: 0; } nav[role="full-horizontal"] ul > li { display: block; float: none; font-size: 1rem; padding: .75rem; margin: 0; } header h1 { font-size: 4rem; } #menu-button { display: inline-block; } ul.dropdown-content { display: none; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <header> <nav role="full-horizontal"> <div class="cf"> <a id="menu-button" href="#">MENU</a> </div> <ul id="menu"> <li><a href="/">Home</a> </li> <li><a href="/work">Work</a> </li> <li><a href="/blog">Articles</a> </li> <li><a href="/contact">Contact</a> </li> <li class="dropdown"> <a href="#"></a> <ul class="dropdown-content"> <li><a href="">Posts Admin</a> </li> <li><a href="">Projects Admin</a> </li> <li><a href="">Categories Admin</a> <li><a href="#">Account Settings</a> </li> <li><a href="">Logout</a> </li> </ul> </li> </ul> </nav> </header> 

Fiddle Demo 小提琴演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM