Pregunta

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?

¿Fue útil?

Solución

You need to return authentication after $.get:

$.get(http_root + o.file, function(data){
...
},"json");

return authentication; 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top