6

I have a variable mutedUser which I would like to have persist to another function. I am having a bit of trouble with the variable persisting outside the click event. What would be the best way to have it so the "return mutedUser" would keep the "muted" string addition based on the conditions of the if statement being met? Thanks!

*The console.log's were me checking to see where the persistance stops

this.isUserMuted = function isUserMuted(payload) {
  var mutedUser = '';
  // If mute button is clicked place them into muted users list
  // check for duplicates in list
  $("#messages-wrapper").off('click', '.message button.muteButton');
  $("#messages-wrapper").on('click', '.message button.muteButton', function(e) {

      $('#unMute').show();

      //create userId reference variable
      var chatUserID = parseInt($(this).parent().parent().attr("data-type"));

      //store userId in muted user object
      mutedUsers[chatUserID] = {};
      mutedUsers[chatUserID].id = chatUserID;
      mutedUsers[chatUserID].muted = true;

      if (mutedUsers[chatUserID] !== null && mutedUsers[chatUserID].id === payload.a) {
          console.log("user is now muted");
          mutedUser += ' muted';
          console.log(mutedUser + 1);
      }
      console.log(mutedUser + 2);
  });
  return mutedUser;
};
1
  • is the if statement where you check for the not null and the id working? besides can you do this and post the result: console.log(mutedUser + 'muted1'); Commented Dec 20, 2012 at 22:04

3 Answers 3

5

If I understood what you're trying to do (by looking at the code), this would be the best approach:

// If mute button is clicked place them into muted users list
// check for duplicates in list
$("#messages-wrapper").off('click', '.message button.muteButton');
$("#messages-wrapper").on('click', '.message button.muteButton', function(e) {
    $('#unMute').show();

    //create userId reference variable
    var chatUserID = parseInt($(this).parent().parent().attr("data-type"));

    //store userId in muted user object
    mutedUsers[chatUserID] = {};
    mutedUsers[chatUserID].id = chatUserID;
    mutedUsers[chatUserID].muted = true;
});

this.isUserMuted = function isUserMuted(payload) {
  var mutedUser = '';

  if (mutedUsers[payload.a] !== null) {
      mutedUser += ' muted';
  }

  return mutedUser;
};

The code retains the array of mutedUsers, and isUserMuted function checks if provided user is in that array. In the code you provided, you would attach a new event handler every time isUserMuted function is called..

The isUserMuted function could even be shortened to:

this.isUserMuted = function isUserMuted(payload) {
  return mutedUsers[payload.a] !== null ? ' muted' : '';
};
Sign up to request clarification or add additional context in comments.

4 Comments

This would have worked great, however I need the variable "chatUserID" in the scope of this.isUserMuted function. This is because, that variable is based on a click event which checks an attribute within the HTML that gets loaded in
In your implementation, chatUserID is effectively equal to payload.a if user really is muted (judging by your if statement - if (mutedUsers[chatUserID] !== null && mutedUsers[chatUserID].id === payload.a) and this line mutedUsers[chatUserID].id = chatUserID; inside the click handler)
ah! this worked once i set it to != vs. !== thanks for your help!
No problem. I'm glad I could have helped you!
3

Edit

Sorry, my mistake. Another way is to pass in that variable, i.e.

this.isUserMuted = function isUserMuted(payload, isMuted) {
  isMuted = '';
  // If mute button is clicked place them into muted users list
  // check for duplicates in list
  $("#messages-wrapper").off('click', '.message button.muteButton');
  $("#messages-wrapper").on('click', '.message button.muteButton', function(e) {

      $('#unMute').show();

      //create userId reference variable
      var chatUserID = parseInt($(this).parent().parent().attr("data-type"));

      //store userId in muted user object
      mutedUsers[chatUserID] = {};
      mutedUsers[chatUserID].id = chatUserID;
      mutedUsers[chatUserID].muted = true;

      if (mutedUsers[chatUserID] !== null && mutedUsers[chatUserID].id === payload.a) {
          console.log("user is now muted");
          isMuted += ' muted';
          console.log(mutedUser + 1);
      }
      console.log(mutedUser + 2);
  });
  return isMuted;
};

3 Comments

Actually your original version was better. The new version doesn't work.
You know what you are right. But I normally see this as a module pattern, i.e. something like jsfiddle.net/VpAPP
this helped me understand the issue more, appreciate it
2

You can't. If you return a string from the function, it will always be passed by value, i.e. copied; and it's value will not change any more. You would need to return a function that can access the current value of the local variable, or an object with a property that is changing.

As you already seem to have an object, option#2 will fit in well here:

function User() { // or whatever you have
    …

    var user = this;
    // If mute button is clicked place them into muted users list
    // check for duplicates in list
    $("#messages-wrapper").on('click', '.message button.muteButton', function(e) {

        $('#unMute').show();

        //store userId in muted user object
        mutedUsers[user.id] = user;
        user.muted = true;
    });
    this.muted = false;
    this.isUserMuted = function() {
        return this.muted ? ' muted' : '';
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.