Reputation: 57176
How can I return some values from a plugin?
Here is my plugin,
(function($){
$.fn.extend({
get_authentication: function(options) {
var defaults = {
file: 'json.php',
location: 'xfolder/',
callback: function(data) {}
}
var options = $.extend(defaults, options);
var o = options;
var $this = this;
var authentication = false;
$.get(http_root + o.file, function(data){
// Send the data out from this plugin.
if(data) {
options.callback(data);
authentication = true;
// alert(authentication); // --> get 'true'
return authentication;
}
// Redirect if the date returns as 'expired'.
if(data && data.error == 'expired') window.location = http_root + o.location;;
},"json");
}
});
})(jQuery);
So, if I get the correct data as json format, I want the plugin can return 'true' automatically. and it also allow callback if I want to get into the json data.
on dom ready,
var data = $.fn.get_authentication();
alert(data); // --> returns 'undefined'
Is it possible?
Upvotes: 0
Views: 205
Reputation: 149
You need to return authentication after $.get
:
$.get(http_root + o.file, function(data){
...
},"json");
return authentication;
Upvotes: 1