2011-04-13 10 views
5

que define mi base plugin en http://docs.jquery.com/Plugins/AuthoringjQuery plugin de llamada a la función pública dentro de otra función pública

(function($){ 

    var methods = { 
    init : function(options) { }, 
    show : function(options) { }, 
    hide : function() { }, 
    update : function(content) { 
     // How to call the show method inside the update method 
     // I tried these but it does not work 
     // Error: not a function 
     this.show(); 
     var arguments = { param: param }; 
     var method = 'show'; 
     // Error: options is undefined 
     methods[ method ].apply(this, Array.prototype.slice.call(arguments, 1)); 
    } 
    }; 

    $.fn.tooltip = function(method) { 

    // Method calling logic 
    if (methods[method]) { 
     return methods[ method ].apply(this, Array.prototype.slice.call(arguments, 1)); 
    } else if (typeof method === 'object' || ! method) { 
     return methods.init.apply(this, arguments); 
    } else { 
     $.error('Method ' + method + ' does not exist on jQuery.tooltip'); 
    }  

    }; 

})(jQuery); 

Cómo llamar al método Show en el interior del método de actualización?

EDITAR:

El método show hace referencia a this. El uso de methods.show(options) o methods['show'](Array.prototype.slice.call(arguments, 1)); funciona para llamar al método show pero luego la referencia a this parece estar equivocada porque recibí un error this.find(...) is not a function.

El método show:

show: function(options) { 
    alert("Options: " + options); 
    alert("Options Type: " + options.interactionType); 
    var typeCapitalized = capitalize(options.interactionType); 
    var errorList = this.find('#report' + typeCapitalized); 
    errorList.html(''); 
}, 

Respuesta

14
var methods = { 
    init : function(options) { }, 
    show : function(options) { }, 
    hide : function() { }, 
    update : function(options) { 

    methods.show.call(this, options); 

    } 
}; 
+0

'methods.show()' 'obras pero no $ (this) .show()' – Sydney

+0

Ok, no estaba tan seguro de '$ (this) .show();'. –

+0

He editado la pregunta porque ahora tengo un problema dentro del método 'show' debido a una referencia a' this' – Sydney

1

La utilización del .apply se lo está tirando todo lo que fuera aquí. Estás deliberadamente cambiando lo que significa this, que es lo que hace que this.show() no funcione. Si quieres this seguir siendo methods (que tiene sentido), se puede hacer simplemente:

return methods[ method ](Array.prototype.slice.call(arguments, 1)); 
Cuestiones relacionadas