Jquery plugin with api
How can I retrieve API variables from a plugin. As p开发者_JS百科er the Jquery Authoring documentation, here is plugin structure:
(function( $ ){
var methods = {
init : function( options ) {
return this.each(function() {
var settings = {
'height' : 10,
'width' : 10
};
if ( options ) { $.extend( settings, options ); }
$('<div>', {
css : {
height : settings.height,
width : settings.width,
}
}).appendTo(this);
});
}
};
$.fn.myPlugin = function( method ) {
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.element' );
}
};
})( jQuery );
I would like to be able to do the following :
var element = $('div').myPlugin();
console.log(element.properties);
In properties, I would have a function that returns the height and width of my Div. How would I go about creating the properties variable in this design ?
thanks
You would get rid of...
return this.each(function() { ... });
...and have your plugin return something such as...
return {
'properties': {
'width': calculatedWidth,
'height': calculatedHeight
}
}
However, doing this will mean you can not chain another method.
精彩评论