Results 1 to 2 of 2

Thread: Storing event methods in button 'data' attribute - Listeners shortcut OOP approach

  1. #1
    Join Date
    May 2010
    Location
    Sacramento, CA
    Posts
    91
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Lightbulb Storing event methods in button 'data' attribute - Listeners shortcut OOP approach

    I'm fascinated with a shortcut for javascript apps and event listeners - i'd like to share this with everyone (newbies especially)!

    On your page you have several buttons:
    HTML Code:
    <button class="add">Add</button>
    <button class="update">Update</button>
    <button class="delete">Delete</button>
    Then you have listeners (I use jQuery):
    Code:
    $('button.add').click(function(){ 
    //do something
    });
    $('button.update').click(function(){ 
    //do something
    });
    $('button.delete').click(function(){ 
    //do something
    });
    ^^^ This can add up to be a lot of code and messy - here's an OO approach to listeners:

    Add data attribute to your buttons:
    HTML Code:
    <button data-method="add">Add</button>
    <button data-method="update">Update</button>
    <button data-method="delete">Delete</button>
    Now, in your javascipt you want to write less listener code so you check for the data-method value and call that method:
    Code:
    //listener for all button clicks
    $('button').click(function(){
    
    var method = $(this).data('method');//get the method/action of the button being clicked
    
    buttonEvents[method]();//call the method of the button in the "buttonEvents" object by the key
    });
    
    var buttonEvents = {
    add: function(){
    //do something
    },
    update: function(){
    //do something
    },
    delete: function(){
    //do something
    }
    };
    There you have it! shortening up the listener code in a jiff!

    Bullets
    The 'buttonEvents' object is the scope you are calling the key/variable expression. Because of this, you do not user the '.' instead use '[]' similar to an array (arrays and objects are very closely related)

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Very nice. data-* attributes are underrated.

    Minor suggestion:
    Code:
    var method = $(this).data('method');//get the method/action of the button being clicked
    You don't have to create a jQuery object, here: it's expensive, and there's no advantage over "vanilla" js. This line could be:
    Code:
    var method = this.getAttribute( 'data-method' );
    Of course, you don't need to use jQuery to assign the event listener, either, but that takes care of some complexity in making things work cross-browser.
    Last edited by traq; 12-22-2013 at 09:51 PM.

  3. The Following User Says Thank You to traq For This Useful Post:

    crobinson42 (12-22-2013)

Similar Threads

  1. storing and retrieving unicode data.
    By james438 in forum MySQL and other databases
    Replies: 7
    Last Post: 01-04-2012, 05:48 AM
  2. Replies: 7
    Last Post: 08-06-2010, 07:25 AM
  3. "send shortcut to desktop" button for website
    By stepheniryna in forum Looking for such a script or service
    Replies: 0
    Last Post: 10-24-2009, 03:00 AM
  4. Storing Checkbox Data
    By resilient in forum MySQL and other databases
    Replies: 1
    Last Post: 02-15-2009, 05:35 PM
  5. Problem with storing data in MySQL
    By costas in forum MySQL and other databases
    Replies: 1
    Last Post: 08-21-2006, 02:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •