Multi Instance Plugin Issue

Multi Instance Plugin Issue

Hey all,
I'm in the process of writing a JQuery Plugin, and have ran into a bit of a snag. When I create multiple instances of a plugin, it always ends up returning the last instance that was created.

Can any of you JQuery wizards help me out?

Here is an example of the code that I'm using. I'm currently testing this in chrome.

  1. <html>
  2. <title>Sample</title>
  3. <head>
  4. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  5. <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.23/jquery-ui.min.js"></script>
  6. <script language="javascript" type="text/javascript">
  7. $(document).ready(function () {

  8. $('#hello').samplePlugin('Hello Plugin');
  9. $('#by').samplePlugin('By Plugin');

  10. $('#clickme').click(function () {
  11. $('#hello').DisplayShowTitle();
  12. $('#by').DisplayShowTitle();
  13. });
  14. });


  15. (function ($) {
  16. $.samplePlugin = function (el, title, options) {
  17. // To avoid scope issues, use 'base' instead of 'this'
  18. // to reference this class from internal events and functions.
  19. var base = this;

  20. // Access to jQuery and DOM versions of element
  21. base.$el = $(el);
  22. base.el = el;

  23. // Add a reverse reference to the DOM object
  24. base.$el.data("samplePlugin", base);

  25. base.init = function () {
  26. if (typeof (title) === "undefined" || title === null) title = "null";

  27. base.title = title;

  28. base.options = $.extend({}, $.samplePlugin.defaultOptions, options);

  29. // Put your initialization code here
  30. };

  31. // Sample Function, Uncomment to use
  32. base.showTitle = function (paramaters) {
  33. return base.title;
  34. };



  35. // Run initializer
  36. base.init();

  37. $.fn.DisplayShowTitle = function () {
  38. var message = base.showTitle();
  39. alert(message);
  40. return message;
  41. };

  42. };


  43. $.samplePlugin.defaultOptions = {
  44. };

  45. $.fn.samplePlugin = function (title, options) {
  46. return this.each(function () {
  47. (new $.samplePlugin(this, title, options));
  48. });
  49. };

  50. })(jQuery);

  51. </script>
  52. </head>
  53. <body>
  54. <div id="hello" name="hello">
  55. Hello</div>
  56. <div id="by" name="by">
  57. By</div>
  58. <input id="clickme" name="clickme" type="button" value="click" />
  59. </body>
  60. </html>

Thanks for any help