Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:09:51+00:00 2026-06-14T06:09:51+00:00

(disclaimer: this question is much simpler than the title suggests!) I have a re-occuring

  • 0

(disclaimer: this question is much simpler than the title suggests!)

I have a re-occuring problem in my program architecture with listening to changes against a model.

I have set-up some collections (much like in Backbone.js) which are just lightweight classes built around an array. The collection is a collection of models. When the user makes changes to a model I keep ending up with dreadful nested events. This is a typical, simple, scenario that I’m trying to avoid:

$("#user-details").on("click", "#save", onSaveClick);

function onSaveClick()
{       
    var givennames = $("#givenname").val(),

    // Get the model from the collection
    var user = users.get($(this).attr("data-id"));

    // set the property
    user.set("GivenNames", givennames);

    // listen for the save event
    $(user).on("save", function(){
        // Alert the view
    });

    user.save();    // save event triggers a "save" event on the model
}

If the same user is saved twice the event gets added/fired multiple times. Is there a better pattern for this?

Should the event be bubbling up through the collection and handled that way perhaps?

Here’s an actual example (the one I’m most ashamed of)

$("#user-devices").on("click", ".device button", function(){

            var button = $(this),
                deviceId = $(this).closest(".device").attr("data-id"),
                device = userDevices.get(deviceId);


            $(device).on("activate", function(event, device){

                button.removeClass("btn-danger").addClass("btn-success")

                $("#activation-dialog").modal("show");
            })

            if (device.get("Active") === "1")
            {
                $("#reactivation-warning-dialog").modal("show");
                $("#reactivation-warning-dialog .btn-primary").on("click", function(){
                    device.activate();
                });

            }
            else
            {
                device.activate();  
            }
        });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:09:53+00:00Added an answer on June 14, 2026 at 6:09 am

    You could just check if the save event is already binded, in that case not bind it again, like this:

    // if no 'save' event already binded
    if (!$(user).data('events') || !$(user).data('events').save) { 
        // listen for the save event
        $(user).on("save", function(){
            // Alert the view
        });
    }
    

    See working example


    To put some sugar on top, we can make the “check if event exist” logic into a custom jquery pseudo-selector, that would be defined as follows:

    $.expr[':'].hasEvent = function(obj, idx, meta, stack) {
        return ($(obj).data('events') != undefined 
                && $(obj).data('events')[meta[3]] != undefined);
    };
    

    Then you can use it this way:

    $(user).not(":hasEvent(save)").on("save", function(){
         // Alert the view
    });
    

    Working example


    UPDATE FOR JQUERY >= 1.8

    Starting with jQuery 1.8 there were some changes to the events object which makes my above code not to work, see this excerpt from the jQuery 1.8 release notes:

    $(element).data(“events”): In version 1.6, jQuery separated its
    internal data from the user’s data to prevent name collisions.
    However, some people were using the internal undocumented “events”
    data structure so we made it possible to still retrieve that via
    .data(). This is now removed in 1.8, but you can still get to the
    events data for debugging purposes via $._data(element, "events").
    Note that this is not a supported public interface; the actual data
    structures may change incompatibly from version to version.

    So here I post the jQuery >= 1.8 updated versions for of my above examples:

    Checking if the save event is already binded, in that case not bind it again:

    // if no 'save' event already binded
    if (!$._data(user, 'events') || !$._data(user, 'events').save) { 
        // listen for the save event
        $(user).on("save", function(){
            // Alert the view
        });
    }
    

    And the custom jquery pseudo-selector:

    $.expr[':'].hasEvent = function(obj, idx, meta, stack) {
        return ($._data(obj, 'events') != undefined 
                && $._data(obj, 'events')[meta[3]] != undefined);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: this question is driven by my personal curiosity more than an actual need
( Disclaimer: This question is not specific to ASP.NET) I have a control which
Disclaimer: this question is purely informational and does not represent an actual problem I'm
DISCLAIMER: This question is only for those who have access to the econometrics toolbox
Disclaimer: I apologize that this question is so long. I have added code as
Disclaimer: this question may not have practical value, it's more of a puzzle/curiosity question.
Disclaimer: This question is strictly academic. The example I'm about to give is probably
Disclaimer: this is not a question about how to install asp.net or an application
(Disclaimer: I realize this is a massive wall of text, but I have done
Not so much a question to help my own programming, but I found this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.