tags:

views:

50

answers:

2

I have a page that loads content via ajax. When I click on a list item it shows a detail view of that item via ajax. In the details i have a lightbox that opens up if a link is clicked. I'm using Colorbox for that.

What boggles me is that if I use $(selector).click() it won't work, nor the $(selector).bind() either. The only way a click can be captured and trigger the Colorbox is if I use the $(selector).live() feature of jQuery.

But by using it, I need to click on the link twice to get the colorbox to activate.

This is what I have:

$('#details #map-work').live('click', function(){
                var name  = $('#dFname').text();
                ///////////////////////////////////////////////////////////////////
                ////////// Google Maps API Functions //////////////////////////////
                $(this).bind('cbox_complete', function(){
                    var geocoder;
                    var map;
                    var a = $("span#co_address").text() + $("span#co_city").text() + $("span#co_state").text() + $("span#co_zip").text();
                    //var image = 'http://code.google.com/apis/maps/documentation/javascript/examples/images/beachflag.png';
                    geocoder = new google.maps.Geocoder();
                    var latlng = new google.maps.LatLng(-34.397, 150.644);
                    var myOptions = {
                        zoom: 19,
                        center: latlng,
                        disableDefaultUI: true,
                        mapTypeId: google.maps.MapTypeId.HYBRID
                    }
                    map = new google.maps.Map(document.getElementById("map"), myOptions);

                    geocoder.geocode( { 'address': a}, function(results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            map.setCenter(results[0].geometry.location);
                            var infowindow = new google.maps.InfoWindow({
                                content: a
                            });
                            var marker = new google.maps.Marker({
                                map: map,
                                //draggable: true,
                                //icon: image,
                                position: results[0].geometry.location
                            });
                            google.maps.event.addListener(marker, 'click', function() {
                                infowindow.open(map, this);
                            });
                            infowindow.open(map, marker);
                        } else {
                            console.log("Geocode was not successful for the following reason: " + status);
                        }
                    });
                });
                ///////////////////////////////////////////////////////////////////
                $(this).colorbox({width:"650", inline:true, href:"#map", overlayClose: false});
                //$('#colorbox').draggable();
                return false
            });

I have been trying to see if I can load this script once the detail view is activated maybe then I can use the click() or bind() instead of live() but that still doesn't work with my scenario or perhaps I just don't understand this very well and don't know something this simple.

+1  A: 

It's a bit difficult to guess the exact problem without a demo page, but you could try this version of your code:

$('#details #map-work')
  .live('cbox_complete', function(){
                    var geocoder;
                    var map;
                    var a = $("span#co_address").text() + $("span#co_city").text() + $("span#co_state").text() + $("span#co_zip").text();
                    //var image = 'http://code.google.com/apis/maps/documentation/javascript/examples/images/beachflag.png';
                    geocoder = new google.maps.Geocoder();
                    var latlng = new google.maps.LatLng(-34.397, 150.644);
                    var myOptions = {
                        zoom: 19,
                        center: latlng,
                        disableDefaultUI: true,
                        mapTypeId: google.maps.MapTypeId.HYBRID
                    }
                    map = new google.maps.Map(document.getElementById("map"), myOptions);

                    geocoder.geocode( { 'address': a}, function(results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            map.setCenter(results[0].geometry.location);
                            var infowindow = new google.maps.InfoWindow({
                                content: a
                            });
                            var marker = new google.maps.Marker({
                                map: map,
                                //draggable: true,
                                //icon: image,
                                position: results[0].geometry.location
                            });
                            google.maps.event.addListener(marker, 'click', function() {
                                infowindow.open(map, this);
                            });
                            infowindow.open(map, marker);
                        } else {
                            console.log("Geocode was not successful for the following reason: " + status);
                        }
                    });
                }
  )
  .live('click', function(){
                ///////////////////////////////////////////////////////////////////
                ////////// Google Maps API Functions //////////////////////////////
                ///////////////////////////////////////////////////////////////////
                $(this).colorbox({width:"650", inline:true, open: true, href:"#map", overlayClose: false});
                //$('#colorbox').draggable();
                return false
            });
Thomas
www.helixagent.com/contacts.php its the in the Contacts tab login is test/password. Click on the Test Contact and the map link by the address will show my issue
s2xi
@s2xi: Could you try my proposed version? I've also noticed that the element's id on your demo page is `map-home` as opposed to `map-work`, so you'd have to adjust for that.
Thomas
hi, sorry in my test account i forgot to input a work address. I duplicated the address over to home and work, so the home map has my original javascript and the work has your proposed version. You can take a look now
s2xi
@s2xi: I've edited the code for a new version to try. What happens in your code is: You click on the map link, **then** you initialize the colorbox plugin. Next time you click on it, the colorbox plugin is initialized and actually opens. So you want to initialize the colorbox as soon as the first ajax content is loaded, so that it can react to the first click by opening.
Thomas
hmmmm, ya I can see the logic in that. But oddly enough I have tried the version you provided in my earlier attempts. I re applied it just to make sure, but not the colorbox will not load.
s2xi
Yeah, sorry, it's a bit of trial and error, because I'm not familiar with the plugin. I have one more easy idea to try, and then I'll have to declare defeat for today. Give me a minute. :)
Thomas
@s2xi: New version is edited. Give it a try and hope for the best. ;)
Thomas
hey, thanks it works. Attaching the live to each other really did the trick. Thank you
s2xi
@s2xi: The relevant change was the option `open: true`. This might be a bit of a dirty fix, though. As of now, the colorbox would be called for every click, and I'm not sure if the plugin checks whether it has already been invoked on an element or not. So if you want it clean and tidy, you'd have to double check there.
Thomas
A: 

Hi

If I have understood the scenario correctly, I see few problems in your code:

  1. You are using an ID selector “#deatils” in context of another ID selector “#map-work”. If your ids are unique then your id selector do not need another context.

  2. You are binding the cbox_complete event before consuming the colorbox plugin. Ideally it should be done from the callback method of the colorbox plug-in to ensure that the event is binded only after the colorbox plugin is consumed.

  3. Secondly, you should be using .unbind(“cbox_complete”).bind(“cbox_complete”, function(){…})

  4. Though this point is not related to the issue that you are facing, yet is something that will help manage your code better. Try to create literal methods and then bind those literals to events instead of anonymous methods.

So your code looks something like this:

var localVariables={

currentSelectedNode:null
}

var functions={

bindDetailView:function(){

    $(“#details”).live(“click”, functions.conumeColorBox);

},
conumeColorBox:function(){
    localVariables.currentSelectedNode=$(this);
    $(this).colorbox({
        //your colorbox options
        }, functions.onColorBoxConsumed);
},
onColorBoxConsumed:function(){
    if(localVariables.currentSelectedNode){
    localVariables.currentSelectedNode.unbind(“cbox_complete”).bind(“cbox_complete” functions.onColorBoxCompleted);
        }
},
onColorBoxCompleted:function(){
    //your whole code for google maps that need to be called on cbox_complete event
}
}

Hope this helps you.

Thanks
Pranav Kaushik
pranavkaushik.wordpress.com

Pranav Kaushik
thanks for the help, but this method is way over my javascript comprehension right now ;-) I'll keep it and study it as it seems to be a more efficient way to reduce my code and load if adapted correctly.
s2xi