Posted By

mikkeldamm on 06/15/12


Tagged

list array Generic


Versions (?)

Generic List in Javascript


 / Published in: JavaScript
 

The closest to a generic List in javascript. When calling constructor - new List(Module) - add model in constructor parameter

  1. /******************/
  2. /*** List class ***/
  3. /******************/
  4. (function () {
  5.  
  6. function List(type) {
  7.  
  8. var _type = type.Type;
  9. var _list = {};
  10. var _count = 0;
  11.  
  12. this.Add = function (obj) {
  13.  
  14. /// <summary>Adds an object to the end of the List</summary>
  15.  
  16. if (obj.Type === _type)
  17. {
  18. _list[obj.Id] = obj;
  19. _count++;
  20. }
  21. else
  22. {
  23. $.error("Cannot convert source type (" + obj.Type + ") to target type (" + _type + ")");
  24. }
  25. };
  26.  
  27. this.Get = function (id) {
  28.  
  29. /// <summary>Get object by id</summary>
  30. /// <param name="id">The id of the object wich to be find</param>
  31. /// <returns type="object">Returns the requested object if exist</returns>
  32.  
  33. if (_list[id] != undefined && _list[id] != null)
  34. return _list[id];
  35.  
  36. return null;
  37. };
  38.  
  39. this.GetAt = function (index) {
  40.  
  41. /// <summary>Get object by index</summary>
  42. /// <param name="id">The index of the object wich to be find</param>
  43. /// <returns type="object">Returns the requested object if exist</returns>
  44.  
  45. if (_list[index] != undefined && _list[index] != null)
  46. return _list[index];
  47.  
  48. return null;
  49. };
  50.  
  51. this.GetAll = function (loop) {
  52.  
  53. /// <summary>Get objects in collection</summary>
  54. /// <param name="loop">The function loop the return to</param>
  55. /// <returns type="object">Returns the collection of objects</returns>
  56.  
  57. for (var item in _list) {
  58. loop(_list[item]);
  59. }
  60. };
  61.  
  62. this.Remove = function (id) {
  63.  
  64. /// <summary>Remove the object with the current id from the List</summary>
  65. /// <param name="id">The id of the object wich to be removed</param>
  66.  
  67. delete _list[id];
  68. _count--;
  69. };
  70.  
  71. this.RemoveAt = function (index) {
  72.  
  73. /// <summary>Remove the object at the index from the list</summary>
  74. /// <param name="index">The index from the list where the object wich to be removed</param>
  75.  
  76. delete _list[index];
  77. _count--;
  78. };
  79.  
  80. this.Length = function () {
  81.  
  82. /// <summary>Get length of the collection</summary>
  83. /// <returns type="object">Returns the length of the collection</returns>
  84.  
  85. return _count;
  86. };
  87.  
  88. }
  89.  
  90. window.List = List;
  91.  
  92. } (window))
  93.  
  94.  
  95.  
  96. /********************/
  97. /*** Module model ***/
  98. /********************/
  99. (function (window) {
  100.  
  101. var type = "Module";
  102.  
  103. function Module(id) {
  104.  
  105. this.Type = type;
  106. this.Id = id;
  107.  
  108. }
  109.  
  110. Module.Type = type;
  111.  
  112. window.Module = Module;
  113.  
  114. } (window));

Report this snippet  

You need to login to post a comment.