API Docs for: 3.10.3
Show:

File: node/js/node-data.js

  1. /**
  2. * Provides methods for managing custom Node data.
  3. *
  4. * @module node
  5. * @main node
  6. * @submodule node-data
  7. */
  8.  
  9. Y.mix(Y.Node.prototype, {
  10. _initData: function() {
  11. if (! ('_data' in this)) {
  12. this._data = {};
  13. }
  14. },
  15.  
  16. /**
  17. * @method getData
  18. * @for Node
  19. * @description Retrieves arbitrary data stored on a Node instance.
  20. * If no data is associated with the Node, it will attempt to retrieve
  21. * a value from the corresponding HTML data attribute. (e.g. node.getData('foo')
  22. * will check node.getAttribute('data-foo')).
  23. * @param {string} name Optional name of the data field to retrieve.
  24. * If no name is given, all data is returned.
  25. * @return {any | Object} Whatever is stored at the given field,
  26. * or an object hash of all fields.
  27. */
  28. getData: function(name) {
  29. this._initData();
  30. var data = this._data,
  31. ret = data;
  32.  
  33. if (arguments.length) { // single field
  34. if (name in data) {
  35. ret = data[name];
  36. } else { // initialize from HTML attribute
  37. ret = this._getDataAttribute(name);
  38. }
  39. } else if (typeof data == 'object' && data !== null) { // all fields
  40. ret = {};
  41. Y.Object.each(data, function(v, n) {
  42. ret[n] = v;
  43. });
  44.  
  45. ret = this._getDataAttributes(ret);
  46. }
  47.  
  48. return ret;
  49.  
  50. },
  51.  
  52. _getDataAttributes: function(ret) {
  53. ret = ret || {};
  54. var i = 0,
  55. attrs = this._node.attributes,
  56. len = attrs.length,
  57. prefix = this.DATA_PREFIX,
  58. prefixLength = prefix.length,
  59. name;
  60.  
  61. while (i < len) {
  62. name = attrs[i].name;
  63. if (name.indexOf(prefix) === 0) {
  64. name = name.substr(prefixLength);
  65. if (!(name in ret)) { // only merge if not already stored
  66. ret[name] = this._getDataAttribute(name);
  67. }
  68. }
  69.  
  70. i += 1;
  71. }
  72.  
  73. return ret;
  74. },
  75.  
  76. _getDataAttribute: function(name) {
  77. name = this.DATA_PREFIX + name;
  78.  
  79. var node = this._node,
  80. attrs = node.attributes,
  81. data = attrs && attrs[name] && attrs[name].value;
  82.  
  83. return data;
  84. },
  85.  
  86. /**
  87. * @method setData
  88. * @for Node
  89. * @description Stores arbitrary data on a Node instance.
  90. * This is not stored with the DOM node.
  91. * @param {string} name The name of the field to set. If no val
  92. * is given, name is treated as the data and overrides any existing data.
  93. * @param {any} val The value to be assigned to the field.
  94. * @chainable
  95. */
  96. setData: function(name, val) {
  97. this._initData();
  98. if (arguments.length > 1) {
  99. this._data[name] = val;
  100. } else {
  101. this._data = name;
  102. }
  103.  
  104. return this;
  105. },
  106.  
  107. /**
  108. * @method clearData
  109. * @for Node
  110. * @description Clears internally stored data.
  111. * @param {string} name The name of the field to clear. If no name
  112. * is given, all data is cleared.
  113. * @chainable
  114. */
  115. clearData: function(name) {
  116. if ('_data' in this) {
  117. if (typeof name != 'undefined') {
  118. delete this._data[name];
  119. } else {
  120. delete this._data;
  121. }
  122. }
  123.  
  124. return this;
  125. }
  126. });
  127.  
  128. Y.mix(Y.NodeList.prototype, {
  129. /**
  130. * @method getData
  131. * @for NodeList
  132. * @description Retrieves arbitrary data stored on each Node instance
  133. * bound to the NodeList.
  134. * @see Node
  135. * @param {string} name Optional name of the data field to retrieve.
  136. * If no name is given, all data is returned.
  137. * @return {Array} An array containing all of the data for each Node instance.
  138. * or an object hash of all fields.
  139. */
  140. getData: function(name) {
  141. var args = (arguments.length) ? [name] : [];
  142. return this._invoke('getData', args, true);
  143. },
  144.  
  145. /**
  146. * @method setData
  147. * @for NodeList
  148. * @description Stores arbitrary data on each Node instance bound to the
  149. * NodeList. This is not stored with the DOM node.
  150. * @param {string} name The name of the field to set. If no name
  151. * is given, name is treated as the data and overrides any existing data.
  152. * @param {any} val The value to be assigned to the field.
  153. * @chainable
  154. */
  155. setData: function(name, val) {
  156. var args = (arguments.length > 1) ? [name, val] : [name];
  157. return this._invoke('setData', args);
  158. },
  159.  
  160. /**
  161. * @method clearData
  162. * @for NodeList
  163. * @description Clears data on all Node instances bound to the NodeList.
  164. * @param {string} name The name of the field to clear. If no name
  165. * is given, all data is cleared.
  166. * @chainable
  167. */
  168. clearData: function(name) {
  169. var args = (arguments.length) ? [name] : [];
  170. return this._invoke('clearData', [name]);
  171. }
  172. });
  173.