views:

645

answers:

3

I'm having a problem getting a 'change' event to register with the following code

var map = function(){

  function addMapTriggers(){
    $("#map_form select").change(getDataWithinBounds);
  }

 return{
  init: function(){
   getDataWithinBounds();
   addMapTriggers();
  }
 };
}();

and in a jquery document.ready

$(function(){
  map.init();
});

So I get my intial data, then every time I change one of the selects, I get the map data again (getDataWithinBounds sends an ajax call). Problem is, it doesn't work, the change event is never added.

However, if, in the console I type map.init(); it does work. This is weird, I don't understand how there is any difference whatsoever? What am I missing here? I'm testing this on safari AND firefox with the exact same behavior

A: 

Why not like this?

function map(){
 return {
  me = this,
  addMapTriggers: function(){
    $("#map_form select").change(getDataWithinBounds);
  },
  init: function(){
   getDataWithinBounds();
   me.addMapTriggers();
  }
 };
}();
Josh Stodola
i'd like addMapTriggers to be private though
brad
Oh geeze, I feel pretty stupid right now! That was so obvious, it's not even funny.
Josh Stodola
Happens to the best of us :P
brad
+1  A: 

Is 'map_form' id of your select? If yes, then you should do

$("#map_form").change(getDataWithinBounds);

'#map_form select' will try to hook up change event to all the select which are present in element specified by #map_form.

EDIT:

Noticed one more thing, map.init doesn't really exist.

In your code, map is a named function which is executed immediately which returns an object which has init method. This doesn't mean that 'map' var has the init method.

You want to try something like this,

var map = function()
{

    function addMapTriggers()
    {
        $("#map_form select").change(getDataWithinBounds);
    }

    return {
        init:  function()
               {
                    getDataWithinBounds();
                addMapTriggers();
            }
    };
}();

In above code, you run an anonymous function which returns an object which has init method and assign the result to map. Now map object will have init method and you can do this:

$(function()
{
  map.init();
});
SolutionYogi
#map_form is the id of the form, so I'm triggering the events on all selects within that form. Also, I noticed that error as well and changed my code to var map = function(){} Still, map.init(); doesn't set the triggers, on document.ready, but it DOES work if I just type it into the console.
brad
Where is getDataWithinBounds defined? Could you post your HTML as well? Also try, $('#map_form select').change(function() { alert('test'); } ); See if you see the alert. If you do see the alert, it means that trigger is not able to resolve getDataWithinBounds method.
SolutionYogi
getDataWithinBounds is defined the same as addMapTriggers, private function in the map object, it's just really long so I omitted it. Also, I tried with just an alert on change() but it doesn't do that either, though it does enter the function addMapTriggers because I log a message when it enters... So I'm still very confused
brad
HTML is<form id="map_form" action="/reports/data_by_bounding_box" method="GET"><select id="filters_metric_id" name="filters[metric][id]"><option value="1">per Employee</option><option value="2">by Area</option><option value="3" selected="selected">Total</option></select>... many more selects</form>
brad
I think the important thing here is, why does it work when I type it in the console? I mean everything MUST be right if that works. It just doesn't make sense that it doesn't work onLoad
brad
As Paolo reported, you have a missing ) in your load code. Could you verify that please?
SolutionYogi
Didn't notice that sorry. I just added my comments above, still doesn't work
brad
brad, the above code should work. I created a test page: http://jsbin.com/uvuka If you change any select, it does call getDataWithinBounds function. Could you try to create a test page on jsbin.com so that we can debug the problem on your end?
SolutionYogi
You know what, I just tested the above code and it works also. It's *pretty much* what I'm using only my map function is quite a bit more complicated. So it's left me stumped, I'll have to go through it with a find tooth comb and see what I find. I'll do that before I take up more of your time, but I'll keep you posted. Thx so much for your time
brad
If you have lots of JS Code, a small mistake here and there, and things may break mysteriously. When that happens, I try to create a simplest working version and add my code step by step to find the problem.
SolutionYogi
A: 

I'm so embarrassed right now and I apologize for wasting everyone's time. Of course the above code works, what's different about what I'm doing is that my site is using the jquery selectbox plugin, which styles the selects by creating a UL with click events to act like a select (not my choice, trust me) Unfortunately, I was calling init before applying the selectbox code, which itself alters the binding of the selects. Putting the selectbox call first, then calling map.init() completely fixed it.

Many apologies and thanks a lot (SolutionYogi in particular) for taking the time to help me out.

brad
No worries. It happens to the best of us. Personally, I get a kick of out of debugging. I think it's one of the best part of programming! :)
SolutionYogi