views:

884

answers:

1

I am having trouble getting my modal window function to bind to some returned data. I am using Cody Lindy's Jquery.DomWindow plugin. Please note that everything functions properly, except for this problem.

The problem occurs when the data that is returned from my jQuery.getJSON call contains a link that is supposed to open up the modal window. The link however, is not recognised by the DOM and therefore is not bound by any function.

My jQuery function is:

$(".deleteThis").live("click", function(){
    var $this = $(this);

    $.getJSON(this.href, function(data) {
     if( data.success == '1') {
     $this.parent().html('<span style="background:#f1ffd6;padding:5px;">' +data.msg+'</span>');         
     } 
     else if( data.success == '2') {
     $this.parent().html('<span style="background:#f1ffd6;padding:5px;">' +data.msg+'</span>');         
     }      
     else {alert("Failure (most likely our fault).");}
    });

    return false;
});

The php script that jQuery sends to contains some database scripting (not shown below as it is not relevant). The relevant php code that returns the JSON array is this:

if($del ==1 ){
    $msg= "$name has been deleted. (<a href='media/delete_coll.php?cid=$cid&delete=0' class='deleteThis' >undo</a>)";
    print json_encode(array("success" => 1,"msg" => $msg));
}

if($del ==0 ){ 
    $msg= "$name <a href='media/editCollab.php?coll=$coll_id' class='absoluteIframeDOMWindow'>edit</a> 
     <a href='media/delete_coll.php?cid=$coll_id&delete=1&coll=$coll' class='deleteThis' > delete</a> | 
     Collaborator has restored.";
     print json_encode(array("success" => 2, "msg" => $msg));
}
+1  A: 

This is happening because the plugin binds the event handlers on page load, and you are then adding dynamic content to the page after the fact. The easiest solution would be to move whatever code you have to instatiate the DomWindow plugin into a function and call it once on page load and again whenever a new link is added. If it starts double binding the already existing links, you'd then have to give the dynamic links a different class and instatiate the second time around for that class.

EDIT:

There seems to be some misunderstanding here, so I'll explain further:

The jQuery live feature (which you are using for .deleteThis) was designed to fix the problem that you are having. You probably already know this and hence you are using it. The above paragraph was because I believe what you are saying is that the DomWindow edit link you are returning is not opening the DomWindow as you might expect. This is because DomWindow does not use live internally to bind the events. So when you initially instatiate DomWindow on page load, the links get bound, but the new ones don't. If you're willing to edit the source code of the plugin you could directly fix this by simply using live, but it is easier (although not as "clean") to just move the code to a function and call it whenever you're adding more content to the page.

Paolo Bergantino
The .live event (which superUntitled is using) should handle any current and future element called ".deleteThis".
Peter J
His problem is not with .deleteThis, his problem is with the edit link with class='absoluteIframeDOMWindow' - I am assuming he left off the part where he calls the DomWindow plugin to make all links with that class do X, but that is where his problem is.
Paolo Bergantino
I created a new function and a new class, and it works. I know I will run into trouble with this in the future, as when there are multiple instances of the "edit | delete" links and potentially multiple "undo" links, I will inevitably run into the "double bind" (the content in the modal window will be called twice, and therefore displayed twice).
superUntitled
What you could do is pass an argument to the function with the class to bind on. By default you can use absoluteIframeDOMWindow, but you can then add something like the current php time(); and add that as a class too. You then call the binding function with the time passed from php as the class selector and voila, only the right things are bound. At that point you might want to consider editing the source code, but it would definitely work. :)
Paolo Bergantino
Also, I am not sure if DomWindow is, but some plugins that do similar things take this into account and don't double bind, so you might be off the hook. I know for sure Thickbox (http://jquery.com/demo/thickbox/) doesn't double bind if you call it multiple times.
Paolo Bergantino