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';
}
});
}