Call a function assigned in jQuery Plugin Options
I made a jQuery plugin. Now i want assign a jQuery animation (with callback) in the options of the jQuery plugin:
(function($){
$.fn.jPl = function(options) {
var $e = $(this);
var defaults = {
showEffect: jQuery.noop,
hideEffect: jQuery.noop,
};
var options = $.extend(defaults, options);
var onSuccess = function ( ) {
$e.options.hideEffect ( function () {
render(function () {
$e.options.showEffect ( function ( ) {
if ( waitTime < options.minWait ) waitTime = options.minWait;
setTimeout ( function ( ) { refreshWall ( ) } , waitTime * 1000 );
waitTime = 0;
});
})
});
}
}})(JQuery);
Call:
$( "#obj" ).jPl ( {
showEffect: $.fn.showEffect,
hideEffect: $.fn.clearEffect,
} ) ;
Now i get this error:
Uncaught ReferenceError: showEffect is not defined
showEffect is defined and when I call it in the Plugin, it works fine.
================= Edit The showEffect and clearEffect is defined in an other JS File:
(function($) {
$.fn.clearEffect = function( callback ) {
return this.each(function() {
$(this).hide ( function ( ) {
if (typeof callback == 'function') { // make sure the callback is a function
开发者_C百科 callback.call(this); // brings the scope to the callback
}
});
});
}
$.fn.showEffect = function( callback ) {
return this.each(function() {
$(this).show ( function ( ) {
if (typeof callback == 'function') { // make sure the callback is a function
callback.call(this); // brings the scope to the callback
}
});
});
}
})(jQuery);
I think, it's an Error with calling the function like this: $e.options.showEffect - but i'm not sure.
Thank you very much.
Based on the code given, your showEffect
function exists within the scope of the plugin but not outside the scope of the plugin.
Unless you have another function called showEffect
that was declared outside the anonymous function used to create the plugin you will get the error you have received.
Your example may be incomplete as I find hard to believe that some code actually works.
$e.options.showEffect
would not work as options is a local variable; you need to declare this.options instead of var options;
var defaults = {
showEffect: function,
hideEffect: function,
};
function should be replaced with jQuery empty function jQuery.noop, as your version may not work in all browsers
But still, if the first error you get its Uncaught ReferenceError: showEffect is not defined, then i think showEffect its not defined in the same scope you call the plugin, as this code throws other errors, but not the one you have :
function showEffect() {
}
function clearEffect() {
}
$("#obj").jPl ( {
showEffect: showEffect,
hideEffect: clearEffect
} ) ;
精彩评论