0

I have a pop up dialog box and I am trying to make it as dynamic as I can so I can send to it any function I want. I wanted to create a callback function so I can pass any function I want and it will do whatever I need on my object (in this example just print something for testing it.

here is what happens when the pop up is being called:

function DisplayPopUp(obj, callback) {


    //On Action Clicked
    $('#actionButton').click(function(e){
       e.preventDefault();
       callback(Obj);
    });
}

And here is the function that activates the PopUp function

$('.delete').click(function(){
    var obj="something something";
    DisplayPopUp(obj,function(){console.log('going to delete');});
});

Somehow that doesn't work and I get from firebug this error:

Obj is not defined

Clearly I do not transfer my function right - how should I do it?

Pointy
  • 405,095
  • 59
  • 585
  • 614
Alon
  • 7,618
  • 18
  • 61
  • 99

4 Answers4

2

You're calling callback(Obj) but your variable name is obj. Check your case.

BZink
  • 7,687
  • 10
  • 37
  • 55
1

When you invoke the callback in DisplayPopup(), pass the parameter obj, not Obj.

pholser
  • 4,908
  • 1
  • 27
  • 37
1

Well, Obj is not defined. It should be obj.

The first thing you should do when you get an error message is to read it. :)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

I see multiple possible issues with this code:

First, the case of Obj is wrong. It should be this:

function DisplayPopUp(obj, callback) {

    //On Action Clicked
    $('#actionButton').click(function(e){
       e.preventDefault();
       callback(obj);
    });
}

But, second I don't think you're using event handlers properly. You're calling DisplayPopup from a click handler. But, in that click handler, you're installing another click handler on another object. Is that really what you want to do? Unless you've left a bunch of code out of here that creates/destroys actionButton or unbinds click handlers, you can easily end up with multiple click handlers on #actionButton each time a delete button is clicked.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • The handlers are on different elements... If I had to guess, I'd say he's implementing an "are you sure?" box. – Lightness Races in Orbit Dec 18 '11 at 16:42
  • I know they are on different elements. But assuming that `'.delete'` can be clicked more than once, they will end up with multiple click handlers on `#actionButton` as a new handler is attached everytime it's called. It's possible that this is handled by the rest of the code, but since we can't see that, I thought I should mention it as a possible issue. – jfriend00 Dec 18 '11 at 16:45
  • I'm going to go ahead and assume that `#actionButton` is created in the gap in `DisplayPopUp`. – Lightness Races in Orbit Dec 18 '11 at 16:46
  • @TomalakGeret'kal - As you say, that's just an assumption. – jfriend00 Dec 18 '11 at 16:47
  • You could also call it an educated guess ;) – Lightness Races in Orbit Dec 18 '11 at 16:49
  • @TomalakGeret'kal - Yes a guess, but in my opinion, you assume way too much skill for the average questioner here on SO. There are plenty of really horrible pieces of JS that people ask about here. I've learned not to assume people know what they're doing and mention many things I see they may be doing wrong. Sometimes it's overkill, sometimes it's exactly what they needed to hear. With a poster like this who is not particularly interactive, it does no good to ask as I will be long gone by the time they respond, so I write about the issues and potential issues I see. It does no harm. – jfriend00 Dec 18 '11 at 16:54
  • Hi, sorry for the late reply - @TomalakGeret'kal is actually right - this is my first time I am trying to implement an "are you sure?" box. I didn't see the idiotic mistake I had between the Obj/obj issue because I started sensing that there are unnecessary multiplying of events and had hard time understanding the code. ".delete" can be clicked several times (it is a problem) - what do I need to write to delete functions before I make new ones to prevent that. and generally - how should I write an "are you sure?" pop up box for several cases like delete something, create something and so on... – Alon Dec 18 '11 at 17:37
  • I posted a question about the delete events issues here: http://stackoverflow.com/questions/8553680/how-to-write-properly-an-are-you-sure-you-want-to-delete-create-etc-dialog-bo – Alon Dec 18 '11 at 18:10