0

I am using jQuery 2.1.4.

I am using the latest iteration of tipsy.

I know that jQuery's .live was deprecated and to use .on. I made the changes to tipsy as found here:

tipsy live does not work with jQuery 1.9.0

with,

if (options.trigger != 'manual') {
    var eventIn  = options.trigger == 'hover' ? 'mouseenter' : 'focus',
        eventOut = options.trigger == 'hover' ? 'mouseleave' : 'blur';
    if(options.live){
      $(this.context).on(eventIn, this.selector, enter).on(eventOut, this.selector, leave);
    } else {
      this.on(eventIn, enter).on(eventOut, leave);
    }
}

This is the Javascript in my page,

$('svg text').tipsy({ 
        gravity: 's', 
        html: true,
        live: true,
        title: function() {
                $.ajax({
                    dataType: 'json',
                    url: "http://myserver/apihistory/emp.php?empid=123456", 
                    success: function(result){
                        alert(result.lastname);
                        return result.lastname;
                    },
                    error: function(result){
                        return 'no good';
                    }
                });
        }
    });

I get "undefined" from tipsy when I hover over the element. I get the correct value with alert. Why can't tipsy see the value? My php file is sending a header for json,

header('Content-Type: application/json');
echo json_encode($results);

and I have json as my dataType, so I am getting an object, not text. I am sending my lastname text (or so I thought) when I return result.lastname.

I know tipsy works because if I take Ajax out and just use "hi." My tooltip works, plus I have it used elsewhere just not with json.

I have checked the headers, and the Javascript object in the browser's development tools (Chrome), and I have manually browsed to the page. It is correct best I can see, correct data and format (or I couldn't alert out result.lastname).

EDIT:

I read the linked SO question and I believe I understand now, the Asynchronous part being my "problem," so I used this knowledge in my .ajax call:

title: function(){
....
    success: function(result){
                        getLinkage(result.lastname);
                    },
   }

//...outside of tipsy               
   function getLinkage(response){
        return response;
    }

But I continue to get undefined. I do not think I am doing my callback correctly. What am I doing wrong?

EDIT:

I changed this. Isn't this a callback? I realize what's going on, but I don't understand the solution. I thought I had a callback here.

Inside tipsy:

    title: function(){
                return getLinkage();  //isn't this the callback?
            }

     //...outside of tipsy              
           function getLinkage(){
$.ajax({
                    dataType: 'json',
                    url: "http://myserver/apihistory/emp.php?empid=123456", 
                    success: function(result){
                        alert(result.lastname);
                        return result.lastname;
                    },
                    error: function(result){
                        return 'no good';
                    }
                });
            }
Community
  • 1
  • 1
johnny
  • 19,272
  • 52
  • 157
  • 259
  • @Andreas I tried to implement the callback, but I am not doing something right. – johnny Sep 23 '15 at 18:56
  • @johnny — The callback (in both the original and updated code) is returning the value (to nowhere) instead of doing something useful with it. – Quentin Sep 23 '15 at 18:59
  • @Quentin I'm not sure what you mean I'm doing wrong. I updated again. – johnny Sep 23 '15 at 21:07
  • You cannot use a return statement in the callback. The code in the callback has to do the work going forwards. It cannot pass the data back, it is too late for that. – Quentin Sep 23 '15 at 21:08
  • How do I get the data back to the original caller in title in tipsy? – johnny Sep 23 '15 at 21:10
  • You can't. It is too late. You need to rethink your approach to the problem. (Maybe get the data using Ajax *before* you call the `tipsy` method) – Quentin Sep 23 '15 at 21:12
  • Oh. Sorry. I will read some more. So the answer is in the other question though? – johnny Sep 23 '15 at 21:13

0 Answers0