プラグインから値を返すにはどうすればよいですか?
これが私のプラグインです。
(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);
したがって、正しいデータを json 形式で取得した場合、プラグインが自動的に「true」を返すようにしたいと考えています。また、json データにアクセスしたい場合は、コールバックも許可します。
dom の準備ができて、
var data = $.fn.get_authentication();
alert(data); // --> returns 'undefined'
出来ますか?