Is there a way to call a method defined in a jQuery plugin directly in my script?
update
(function($){
$.fn.myPlugin = function (options) {
return this.each(function() {
function doSomething() {
// do something
}
function doSomethingElse() {
// do something else
}
doSomething();
doSomethingElse();
});
}
})(window.jQuery);
now i want to call doSomethingElse() from my script. Is there a way to do this?
Is there a way to call a method defined in a jQuery plugin directly in my script?
update
(function($){
$.fn.myPlugin = function (options) {
return this.each(function() {
function doSomething() {
// do something
}
function doSomethingElse() {
// do something else
}
doSomething();
doSomethingElse();
});
}
})(window.jQuery);
now i want to call doSomethingElse() from my script. Is there a way to do this?
Share edited Oct 19, 2010 at 14:16 dantz asked Oct 19, 2010 at 13:56 dantzdantz 1,8624 gold badges21 silver badges25 bronze badges 2- hope this link help forum.jquery./topic/… – Haim Evgi Commented Oct 19, 2010 at 14:03
- 1 Plugins usually use closures and give public access to only some variables (options) that need to be changed as per requirement which means that the methods are usually private and cannot be called from outside. But by introducing new options, you can extend or modify the plugin as per your requirement. – naiquevin Commented Oct 19, 2010 at 14:07
4 Answers
Reset to default 4The direct answer to your question is, "no". As posted, that code explicitly hides those functions from the outside world.
One jQuery-ish way to make those functions available is to define them differently. Personally, I like to create plugins that have a corresponding "global" object, so if I have
$.fn.pointy = function() {
// plugin
}
I'll also have:
$.pointy = {
something: function() { ... },
somethingElse: function() { ... }
}
That's nice for providing both relevant utilities and also configuration APIs. Thus you could put your "doSomething" functions there, and then use them inside your plugin by qualifying the names as $.myPlugin.doSomething()
. Of course you can also create the functions inside your plugin initialization, so that they might be closures with access to other private functions.
In my opinion best is to use event system when you nedd to call it from outside. Here is the result with your code:
(function($){
$.fn.myPlugin = function (options) {
return this.each(function() {
var $this = $(this);
$this.on('doSomething', function() {
// do something
}
$this.on('doSomethingElse', function() {
// do something else
}
$this.trigger('doSomethingElse');
$this.trigger('doSomething');
});
}
})(window.jQuery);
or call it from outisde with same method:
$('.myPluginDiv').trigger('doSomethingElse');
$('.myPluginDiv').trigger('doSomething');
I'm not a 100% sure i get you, but after defining a new function you can still cal it outside of the jQuery Selection, see:
$.fn.myfunc = function(){
alert('foo');
}
$.fn.myfunc(); // works
$('#something').myfunc(); // works too
So you would have to find out exactly how the methods needs to be called and then do it via $.fn.func()
You can also use events so the plugin is listening for an event with bind and then you call trigger. Could be a target like window etc.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744913083a4600670.html
评论列表(0条)