Skip to content

Instantly share code, notes, and snippets.

@melechi
Forked from deanrather/InstanceTracker.js
Created July 3, 2012 03:42
Show Gist options
  • Save melechi/3037466 to your computer and use it in GitHub Desktop.
Save melechi/3037466 to your computer and use it in GitHub Desktop.
$JSKK.Class.create
(
{
$namespace: 'helper',
$name: 'InstanceTracker'
}
)
(
// Static Block
{
},
// Instance Block
{
_instances: [],
_nextID: 1,
length: 0,
add: function(instance)
{
this._instances.push(instance);
this.length++;
instance.set('id', this._nextID++);
},
getByID: function(id)
{
for(var i in this._instances)
{
var instance = this._instances[i];
if(typeof instance != 'object') continue;
if(instance.get('id') == id) return instance;
}
throw new Error('no instance found with id', id);
},
removeByID: function(id)
{
for(var i in this._instances)
{
var instance = this._instances[i];
if(typeof instance != 'object') continue;
if(instance.get('id') == id)
{
this._instances.splice(i, 1);
this.length--;
return true;
}
}
throw new Error('no instance found with id', id);
},
deleteByID: function(id)
{
for(var i in this._instances)
{
var instance = this._instances[i];
if(typeof instance != 'object') continue;
if(instance.get('id') == id)
{
delete this._instances[i];
this.length--;
return true;
}
}
throw new Error('no instance found with id', id);
},
_each: function(iterator)
{
this._instances.each(iterator);
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment