Guest User

Untitled

a guest
Nov 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. $JSKK.Class.create
  2. (
  3. {
  4. $namespace: 'helper',
  5. $name: 'InstanceTracker'
  6. }
  7. )
  8. (
  9. // Static Block
  10. {
  11.  
  12. },
  13.  
  14. // Instance Block
  15. {
  16. _instances: [],
  17. _nextID: 1,
  18. length: 0,
  19.  
  20. add: function(instance)
  21. {
  22. this._instances.push(instance);
  23. this.length++;
  24. instance.set('id', this._nextID++);
  25. },
  26.  
  27. getByID: function(id)
  28. {
  29. for(var i in this._instances)
  30. {
  31. var instance = this._instances[i];
  32. if(typeof instance != 'object') continue;
  33.  
  34. if(instance.get('id') == id) return instance;
  35. }
  36. throw new Error('no instance found with id', id);
  37. },
  38.  
  39. removeByID: function(id)
  40. {
  41. for(var i in this._instances)
  42. {
  43. var instance = this._instances[i];
  44. if(typeof instance != 'object') continue;
  45.  
  46. if(instance.get('id') == id)
  47. {
  48. this._instances.splice(i, 1);
  49. this.length--;
  50. return true;
  51. }
  52. }
  53. throw new Error('no instance found with id', id);
  54. },
  55.  
  56. deleteByID: function(id)
  57. {
  58. for(var i in this._instances)
  59. {
  60. var instance = this._instances[i];
  61. if(typeof instance != 'object') continue;
  62.  
  63. if(instance.get('id') == id)
  64. {
  65. delete this._instances[i];
  66. this.length--;
  67. return true;
  68. }
  69. }
  70. throw new Error('no instance found with id', id);
  71. },
  72.  
  73. _each: function()
  74. {
  75. // todo: magix
  76. }
  77. }
  78. );
Add Comment
Please, Sign In to add comment