addyosmani / essential-js-design-patterns

Repo for my 'Learning JavaScript Design Patterns' book
http://addyosmani.com/resources/essentialjsdesignpatterns/book/
4.8k stars 791 forks source link

Flyweight Example 1: Centralized event handling code error #182

Closed rsiemens closed 7 years ago

rsiemens commented 8 years ago

The method stateManager.handleClick elem paramater is a DOM element and not a jQuery object. Running .find() on it will throw an error Uncaught TypeError: elem.find is not a function.

var stateManager = {

  fly: function () {

    var self = this;

    $( "#container" )
          .unbind()
          .on( "click", "div.toggle", function ( e ) {
            self.handleClick( e.target );
          });
  },

  handleClick: function ( elem ) {
    elem.find( "span" ).toggle( "slow" );  // Should be $( elem ).find()
  }
};