Added a mutation listener to CompositionCollection (#2354)

This commit is contained in:
Andrew Henry 2019-04-05 09:32:58 -07:00 committed by Deep Tailor
parent 6f3e2a8fbb
commit 6c0c1df010

View File

@ -25,7 +25,6 @@ define([
], function ( ], function (
_ _
) { ) {
/** /**
* A CompositionCollection represents the list of domain objects contained * A CompositionCollection represents the list of domain objects contained
* by another domain object. It provides methods for loading this * by another domain object. It provides methods for loading this
@ -63,7 +62,6 @@ define([
this.onProviderRemove = this.onProviderRemove.bind(this); this.onProviderRemove = this.onProviderRemove.bind(this);
} }
/** /**
* Listen for changes to this composition. Supports 'add', 'remove', and * Listen for changes to this composition. Supports 'add', 'remove', and
* 'load' events. * 'load' events.
@ -76,7 +74,11 @@ define([
if (!this.listeners[event]) { if (!this.listeners[event]) {
throw new Error('Event not supported by composition: ' + event); throw new Error('Event not supported by composition: ' + event);
} }
if (!this.mutationListener) {
this.mutationListener = this.publicAPI.objects.observe(this.domainObject, '*', (newDomainObject) => {
this.domainObject = newDomainObject;
})
}
if (this.provider.on && this.provider.off) { if (this.provider.on && this.provider.off) {
if (event === 'add') { if (event === 'add') {
this.provider.on( this.provider.on(
@ -132,6 +134,10 @@ define([
this.listeners[event].splice(index, 1); this.listeners[event].splice(index, 1);
if (this.listeners[event].length === 0) { if (this.listeners[event].length === 0) {
if (this.mutationListener) {
this.mutationListener();
delete this.mutationListener;
}
// Remove provider listener if this is the last callback to // Remove provider listener if this is the last callback to
// be removed. // be removed.
if (this.provider.off && this.provider.on) { if (this.provider.off && this.provider.on) {