Make sure items removed from DOM remove all their events and reinstall them again when reinserted.

Closes #254.
This commit is contained in:
Jürg Lehni 2013-12-08 20:40:30 +01:00
parent 97fcd6ff38
commit 24ce35cc6e
3 changed files with 42 additions and 16 deletions

View file

@ -115,6 +115,19 @@ var Callback = {
off: '#detach',
trigger: '#fire',
_installEvents: function(install) {
var handlers = this._handlers,
key = install ? 'install' : 'uninstall';
for (var type in handlers) {
if (handlers[type].length > 0) {
var entry = this._eventTypes[type],
func = entry[key];
if (func)
func.call(this, type);
}
}
},
statics: {
// Override inject() so that sub-classes automatically add the accessors
// for the event handler functions (e.g. #onMouseDown) for each property

View file

@ -1109,20 +1109,33 @@ var Item = Base.extend(Callback, /** @lends Item# */{
return this._project;
},
_setProject: function(project) {
if (this._project != project) {
var hasOnFrame = this.responds('frame');
if (hasOnFrame)
this._animateItem(false);
_setProject: function(project, installEvents) {
if (this._project !== project) {
// Uninstall events before switching project, then install them
// again.
if (this._project)
this._installEvents(false);
this._project = project;
if (hasOnFrame)
this._animateItem(true);
if (this._children) {
for (var i = 0, l = this._children.length; i < l; i++) {
this._children[i]._setProject(project);
}
}
var children = this._children;
for (var i = 0, l = children && children.length; i < l; i++)
children[i]._setProject(project);
// We need to call _installEvents(true) again, but merge it with
// handling of installEvents argument below.
installEvents = true;
}
if (installEvents)
this._installEvents(true);
},
/**
* Overrides Callback#_installEvents to also call _installEvents on all
* children.
*/
_installEvents: function _installEvents(install) {
_installEvents.base.call(this, install);
var children = this._children;
for (var i = 0, l = children && children.length; i < l; i++)
children[i]._installEvents(install);
},
/**
@ -1862,7 +1875,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{
for (var i = 0, l = items.length; i < l; i++) {
var item = items[i];
item._parent = this;
item._setProject(this._project);
item._setProject(this._project, true);
// Setting the name again makes sure all name lookup structures
// are kept in sync.
if (item._name)
@ -2016,8 +2029,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{
this._removeNamed();
if (this._index != null)
Base.splice(this._parent._children, null, this._index, 1);
if (this.responds('frame'))
this._animateItem(false);
this._installEvents(false);
// Notify parent of changed hierarchy
if (notify)
this._parent._changed(/*#=*/ Change.HIERARCHY);

View file

@ -86,6 +86,7 @@ var Layer = Group.extend(/** @lends Layer# */{
this._project.activeLayer = this.getNextSibling()
|| this.getPreviousSibling();
Base.splice(this._project.layers, null, this._index, 1);
this._installEvents(false);
// Tell project we need a redraw. This is similar to _changed()
// mechanism.
this._project._needsUpdate = true;
@ -130,7 +131,7 @@ var Layer = Group.extend(/** @lends Layer# */{
this._remove(true);
Base.splice(item._project.layers, [this],
item._index + (above ? 1 : 0), 0);
this._setProject(item._project);
this._setProject(item._project, true);
return this;
}
return _insert.base.call(this, above, item, _preserve);