API Docs for: 3.13.0
Show:

File: collection/js/arraylist-add.js

  1. /**
  2. * Collection utilities beyond what is provided in the YUI core
  3. * @module collection
  4. * @submodule arraylist-add
  5. * @deprecated Use ModelList or a custom ArrayList subclass
  6. */
  7.  
  8. /*
  9. * Adds methods add and remove to Y.ArrayList
  10. */
  11. Y.mix(Y.ArrayList.prototype, {
  12.  
  13. /**
  14. * Add a single item to the ArrayList. Does not prevent duplicates.
  15. *
  16. * @method add
  17. * @param { mixed } item Item presumably of the same type as others in the
  18. * ArrayList.
  19. * @param {Number} index (Optional.) Number representing the position at
  20. * which the item should be inserted.
  21. * @return {ArrayList} the instance.
  22. * @for ArrayList
  23. * @deprecated Use ModelList or a custom ArrayList subclass
  24. * @chainable
  25. */
  26. add: function(item, index) {
  27. var items = this._items;
  28.  
  29. if (Y.Lang.isNumber(index)) {
  30. items.splice(index, 0, item);
  31. }
  32. else {
  33. items.push(item);
  34. }
  35.  
  36. return this;
  37. },
  38.  
  39. /**
  40. * Removes first or all occurrences of an item to the ArrayList. If a
  41. * comparator is not provided, uses itemsAreEqual method to determine
  42. * matches.
  43. *
  44. * @method remove
  45. * @param { mixed } needle Item to find and remove from the list.
  46. * @param { Boolean } all If true, remove all occurrences.
  47. * @param { Function } comparator optional a/b function to test equivalence.
  48. * @return {ArrayList} the instance.
  49. * @for ArrayList
  50. * @deprecated Use ModelList or a custom ArrayList subclass
  51. * @chainable
  52. */
  53. remove: function(needle, all, comparator) {
  54. comparator = comparator || this.itemsAreEqual;
  55.  
  56. for (var i = this._items.length - 1; i >= 0; --i) {
  57. if (comparator.call(this, needle, this.item(i))) {
  58. this._items.splice(i, 1);
  59. if (!all) {
  60. break;
  61. }
  62. }
  63. }
  64.  
  65. return this;
  66. },
  67.  
  68. /**
  69. * Default comparator for items stored in this list. Used by remove().
  70. *
  71. * @method itemsAreEqual
  72. * @param { mixed } a item to test equivalence with.
  73. * @param { mixed } b other item to test equivalance.
  74. * @return { Boolean } true if items are deemed equivalent.
  75. * @for ArrayList
  76. * @deprecated Use ModelList or a custom ArrayList subclass
  77. */
  78. itemsAreEqual: function(a, b) {
  79. return a === b;
  80. }
  81.  
  82. });
  83.