jquery plugin getter methods
I've been looking at starter (http://starter.pixelgraphics.us/) to, well, start making plugins...
So I was wondering - how would I add a method in the sample code that allows me to get a value - breaking the chain yep, but getting a value all the same?
eg. say you wrote a plugin which does stuff to a开发者_Go百科 table, and that plugin keeps internally the number of rows in that table...So what would a method look like/how would you call it to retrieve the row count?
Actually be kind of nice to know how to call methods in general using starter - like say you have a 'foo' method - does 1 thing to a table, and a 'bar' method - retrieves some value - how do you go about that?
Poor example I know, but it's the process I'm after...
Ta
Calvin
If you code base is already using jQuery UI you should use the widget factory. It's really helpful when making jQuery plugins.
A great starting point I've found is:
http://ajpiano.com/widgetfactory/#slide1
jQuery maintains chainability by returning this (the element with jQuery object). As the function you describe would be a getter it would not return the jQuery object it will not be able to be used to continue the chain.
E.G. test using css() and width()
$('h1').css('color').width()
This will break as css('color') returns the color.
Check out the maintaining chainability section of authoring jQuery plugins:
http://docs.jquery.com/Plugins/Authoring#Maintaining_Chainability
精彩评论