Posted By

nerdfiles on 02/16/11


Tagged

jquery


Versions (?)

Plugin Pattern #1


 / Published in: jQuery
 

Element level overrides; the elemental awareness? what the d&d?

  1. HTML:
  2.  
  3. <dl class="tabbedDL" data-settings='{ "sup": "dudes" }'>
  4. <dt>Term</dt>
  5. <dd>Description</dd>
  6. <dt>Term</dt>
  7. <dd>Description</dd>
  8. </dl>
  9.  
  10. JS:
  11.  
  12. ;(function($, window) {
  13.  
  14. var document = window.document,
  15. _config = {
  16. debugMode: true
  17. };
  18.  
  19. console.note = function(o) {
  20.  
  21. if (_config.debugMode) {
  22. console.log(o);
  23. }
  24.  
  25. if (_config.debugMode && (typeof(console === "undefined") || typeof(console.log == "undefined"))) {
  26. // var console; variable hoist'd; woops
  27. console.log = function() {};
  28. }
  29. };
  30.  
  31. $.tabbedDL = function(c, _params) {
  32.  
  33. var $self = $(c),
  34.  
  35. // internal defaults (test defaults);
  36. _defaults = {
  37. whatever: 1
  38. },
  39.  
  40. // internal settings
  41. _settings = (function(_params) {
  42. console.note('Set by argument parameters (init)');
  43. _params['settingsType'] = 'P';
  44. return _params;
  45. })(_params)
  46. || (function(_defaults) {
  47. //console.note('Set by plugin defaults (init)');
  48. _defaults['settingsType'] = 'D';
  49. return _defaults;
  50. })(_defaults);
  51.  
  52. if (typeof($self.attr("data-settings")) === 'undefined'
  53. || (typeof($self.attr("data-settings")) === 'string'
  54. && String($self.attr("data-settings")) === '' )) {
  55.  
  56. $self.data("data-settings", _settings);
  57.  
  58. }
  59.  
  60. return $self.each(function(index, e) {
  61.  
  62. var $self = $(this),
  63.  
  64. // new pattern?
  65. _settings = ($self.data("data-settings"))
  66. ? $self.data("data-settings")
  67. : (function() {
  68. //console.note("Set by element meta-data (override)");
  69. var em = $.parseJSON($self.attr("data-settings"));
  70. em['settingsType'] = 'E';
  71. return em;
  72. }());
  73.  
  74. //console.note(_settings);
  75. //console.log(typeof(_settings));
  76.  
  77. });
  78.  
  79. };
  80.  
  81. })(jQuery, window);
  82.  
  83. $.tabbedDL("dl.tabbedDL", { monkey: 2 });
  84.  
  85. $(function() {
  86. //console.log('ee');
  87. });

Report this snippet  

You need to login to post a comment.