Created
July 3, 2012 03:40
-
-
Save deanrather/3037450 to your computer and use it in GitHub Desktop.
JSKK Instance Tracker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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() | |
{ | |
// todo: magix | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment