最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery click event not working while using "Module Pattern" - Stack Overflow

programmeradmin0浏览0评论

I'm an intermediate front-end JS developer and I'm trying the Module Pattern outlined by Chris Coyyer here.

But when I store a jQuery selector in the settings, I'm unable to use it to trigger a click event. See the below code with my ments... Any help is greatly appreciated!

var s,
TestWidget = {
  settings: {
    testButton: $("#testing")
  },
  init: function() {
    s = this.settings;
    this.bindUIActions();
  },
  bindUIActions: function() {
    console.log(s.testButton); // This works: [context: document, selector: "#testing", constructor: function, init: function, selector: ""…]

    //This doesn't work - why?????
    s.testButton.click(function() {
        //Why isn't this triggered?
        alert('testButton clicked');
    });

    /*This works, obviously:
    $('#testing').click(function() {
        alert('testButton clicked');
    });
    */

  }
};
$(document).ready(function() {
    TestWidget.init();
});

I'm an intermediate front-end JS developer and I'm trying the Module Pattern outlined by Chris Coyyer here.

But when I store a jQuery selector in the settings, I'm unable to use it to trigger a click event. See the below code with my ments... Any help is greatly appreciated!

var s,
TestWidget = {
  settings: {
    testButton: $("#testing")
  },
  init: function() {
    s = this.settings;
    this.bindUIActions();
  },
  bindUIActions: function() {
    console.log(s.testButton); // This works: [context: document, selector: "#testing", constructor: function, init: function, selector: ""…]

    //This doesn't work - why?????
    s.testButton.click(function() {
        //Why isn't this triggered?
        alert('testButton clicked');
    });

    /*This works, obviously:
    $('#testing').click(function() {
        alert('testButton clicked');
    });
    */

  }
};
$(document).ready(function() {
    TestWidget.init();
});
Share Improve this question asked Jul 19, 2013 at 15:40 user1399181user1399181 3134 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

The problem is that you initialize $("#testing") before the DOM is ready, so this jQuery object is empty.

A simple solution is to put all your code in the ready callback.

Another one would be to replace

  settings: {
    testButton: $("#testing")
  },
  init: function() {
    s = this.settings;
    this.bindUIActions();
  },

with

  settings: {
  },
  init: function() {
    s = this.settings;
    s.testButton = $("#testing");
    this.bindUIActions();
  },

But it's hard to get why you use so much code for such a simple thing. You might be overusing the pattern here and it's not really clean as you have two global variables s and TestWidget when one would already be a lot.

Here's a slight variation of your code which would be, in my opinion, cleaner, while still using modules (IIFE variant) :

TestWidget = (function(){
    var settings = {};
    return {
        init: function() {
            settings.testButton = $("#testing");
            this.bindUIActions();
        },
        bindUIActions: function() {
            console.log(settings.testButton);
            settings.testButton.click(function() {
                alert('testButton clicked');
            });
        }
    }

})();
$(document).ready(function() {
    TestWidget.init();
});

settings is kept in the closure and doesn't leak in the global namespace. Note that even this version doesn't make sense if you don't do more with the module.

发布评论

评论列表(0)

  1. 暂无评论