Ich weiß, das wurde mehrmals gefragt, aber irgendwie kann ich keine Antwort finden, die meinen Bedürfnissen entspricht, also hier geht es.jQuery Plugin - Zugriffsmethode des instanziierten Objekts
Ich habe ein Plugin, das wie folgt aussieht (abgespeckte):
(function(window, $){
var MyPlugin = function(elem, options){
this.elem = elem;
this.$elem = $(elem);
this.options = options;
this.metadata = this.$elem.data('popup-options');
this.markup = '<div>some markup here</div>';
this.popup = null;
};
MyPlugin.prototype = {
defaults:{
type: 'outer',
color: 'orange'
},
init: function(){
this.config = $.extend({}, this.defaults, this.options, this.metadata);
this.show();
return this;
},
show: function(){
// some stuff to show the element
},
hide: function(){
// some stuff to hide the element
},
someMethod: function(){
// some other stuff the plugin does
}
};
MyPlugin.defaults = MyPlugin.prototype.defaults;
$.fn.myPlugin = function(options){
return this.each(function(){
var thePlugin = new MyPlugin(this, options).init();
});
};
window.MyPlugin = MyPlugin;
}(window, jQuery));
Jetzt im Code wende ich das Plugin wie folgt aus:
var thePlugin = $(this).myPlugin();
Bisher dies funktioniert.
Aber jetzt möchte ich auf eine der Funktionen innerhalb des Plugins von außerhalb zugreifen (z. B. «someMethod»).
Ich habe versucht, es wie zu nennen:
thePlugin.someMethod();
oder thePlugin.myPlugin.someMethod();
oder sogar
thePlugin.myPlugin[0].someMethod();
nichts davon funktioniert.
Wie muss ich das Plugin erstellen, damit ich nach der Initialisierung darauf zugreifen kann? Ist das überhaupt möglich?
haben Sie versucht 'myPlugin.prototype.someMethod()'? –
versuchte das, hat nicht funktioniert ... :( – Swissdude