Clean up Item#name code.

This commit is contained in:
Jonathan Puckey 2011-05-15 19:27:32 +02:00
parent 16ab461fde
commit a9fbc41e15

View file

@ -45,12 +45,12 @@ var Item = this.Item = Base.extend({
}, },
_removeFromNamed: function() { _removeFromNamed: function() {
var children = this._parent._children; var children = this._parent._children,
var namedChildren = this._parent._namedChildren; namedChildren = this._parent._namedChildren,
var name = this._name; name = this._name,
namedArray = namedChildren[name];
if (children[name] = this) if (children[name] = this)
delete children[name]; delete children[name];
var namedArray = namedChildren[name];
namedArray.splice(namedArray.indexOf(this), 1); namedArray.splice(namedArray.indexOf(this), 1);
// If there are any items left in the named array, set // If there are any items left in the named array, set
// the last of them to be this.parent.children[this.name] // the last of them to be this.parent.children[this.name]
@ -66,29 +66,24 @@ var Item = this.Item = Base.extend({
* The name of the item. * The name of the item.
*/ */
getName: function() { getName: function() {
if (this._name)
return this._name; return this._name;
}, },
setName: function(name) { setName: function(name) {
var children = this._parent.children; var children = this._parent._children,
var namedChildren = this._parent._namedChildren; namedChildren = this._parent._namedChildren;
if (name != this.name) { if (name != this._name) {
// If the item already had a name, // If the item already had a name,
// remove its property from the parent's children object: // remove its property from the parent's children object:
if (this._name) if (this._name)
this._removeFromNamed(); this._removeFromNamed();
this._name = name ? name : undefined; this._name = name || undefined;
} }
if (name) { if (name) {
if (!namedChildren[name]) { (namedChildren[name] = namedChildren[name] || []).push(this);
namedChildren[name] = [this];
} else {
namedChildren[name].push(this);
}
children[name] = this; children[name] = this;
} else { } else {
children[name] = undefined; delete children[name];
} }
}, },