Skip to content

Instantly share code, notes, and snippets.

@erichocean
Created September 5, 2008 05:19
Show Gist options
  • Save erichocean/8927 to your computer and use it in GitHub Desktop.
Save erichocean/8927 to your computer and use it in GitHub Desktop.
/**
Returns the name of this class. If the name is not known, triggers
a search. This can be expensive the first time it is called.
*/
objectClassName: function() {
if (!this._objectClassName) this._findObjectClassNames() ;
if (this._objectClassName) return this._objectClassName ;
var ret = this ;
while(ret && !ret._objectClassName) ret = ret._type;
return (ret && ret._objectClassName) ? ret._objectClassName : 'Anonymous' ;
},
/** @private
This is a way of performing brute-force introspection. This searches
through all the top-level properties looking for classes. When it finds
one, it saves the class path name.
*/
_findObjectClassNames: function() {
if (SC._foundObjectClassNames) return ;
SC._foundObjectClassNames = true ;
var seen = [] ;
var searchObject = function(root, object, levels) {
levels-- ;
// not the fastest, but safe
if (seen.indexOf(object) >= 0) return ;
seen.push(object) ;
for(var key in object) {
if (key == '__scope__') continue ;
if (key == '_type') continue ;
if (!key.match(/^[A-Z0-9]/)) continue ;
var path = (root) ? [root,key].join('.') : key ;
var value = object[key] ;
switch($type(value)) {
case T_CLASS:
if (!value._objectClassName) value._objectClassName = path;
if (levels>=0) searchObject(path, value, levels) ;
break ;
case T_OBJECT:
if (levels>=0) searchObject(path, value, levels) ;
break ;
case T_HASH:
if (((root != null) || (path=='SC')) && (levels>=0)) searchObject(path, value, levels) ;
break ;
default:
break;
}
}
} ;
searchObject(null, window, 2) ;
},
toString: function() { return this.objectClassName(); },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment