From f74225f49b6ca65a3371824b1de8d5c30689f4f0 Mon Sep 17 00:00:00 2001 From: Bob Zeleznik Date: Mon, 18 Mar 2019 02:37:57 -0400 Subject: fixed list bug .. sort of. --- .../views/collections/CollectionViewBase.tsx | 3 +- src/fields/ListField.ts | 44 +++++++++++----------- 2 files changed, 25 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/client/views/collections/CollectionViewBase.tsx b/src/client/views/collections/CollectionViewBase.tsx index 3a6d1c141..fd0e84fb1 100644 --- a/src/client/views/collections/CollectionViewBase.tsx +++ b/src/client/views/collections/CollectionViewBase.tsx @@ -61,10 +61,11 @@ export class CollectionViewBase extends React.Component } else if (docView) { if (doc && docView.props.RemoveDocument && docView.props.ContainingCollectionView !== this.props.CollectionView) { docView.props.RemoveDocument(doc); + this.props.removeDocument(doc); // bcz: not good -- want to check if it's there and then add if it isn't this.props.addDocument(doc); } } else if (doc) { - // this.props.removeDocument(doc); bcz: causes an exception + this.props.removeDocument(doc); // bcz: not good -- want to check if it's there and then add if it isn't this.props.addDocument(doc); } e.stopPropagation(); diff --git a/src/fields/ListField.ts b/src/fields/ListField.ts index ce32da0a6..77c1d6e14 100644 --- a/src/fields/ListField.ts +++ b/src/fields/ListField.ts @@ -16,6 +16,8 @@ export class ListField extends BasicField { this.observeList(); } + private _processingServerUpdate: boolean = false; + private observeDisposer: Lambda | undefined; private observeList(): void { this.observeDisposer = observe(this.Data as IObservableArray, (change: IArrayChange | IArraySplice) => { @@ -31,7 +33,8 @@ export class ListField extends BasicField { redo: () => this.Data[change.index] = change.newValue }) } - Server.UpdateField(this); + if (!this._processingServerUpdate) + Server.UpdateField(this); }); } @@ -69,30 +72,29 @@ export class ListField extends BasicField { init(callback: (field: Field) => any) { Server.GetFields(this._proxies, action((fields: { [index: string]: Field }) => { - if (!this.arraysEqual(this._proxies, this.Data.map(field => field.Id))) { + if (!this.arraysEqual(this._proxies, this.data.map(field => field.Id))) { var dataids = this.data.map(d => d.Id); - var added = this.data.length == this._proxies.length - 1; + var proxies = this._proxies.map(p => p); + var added = this.data.length < this._proxies.length; var deleted = this.data.length > this._proxies.length; for (let i = 0; i < dataids.length && added; i++) - added = this._proxies.indexOf(dataids[i]) != -1; + added = proxies.indexOf(dataids[i]) != -1; for (let i = 0; i < this._proxies.length && deleted; i++) - deleted = dataids.indexOf(this._proxies[i]) != -1; - if (added) { // if only 1 items was added - for (let i = 0; i < this._proxies.length; i++) - if (dataids.indexOf(this._proxies[i]) === -1) - this.Data.splice(i, 0, fields[this._proxies[i]] as T); - } else if (deleted) { // if only items were deleted - for (let i = this.data.length - 1; i >= 0; i--) { - if (this._proxies.indexOf(this.data[i].Id) === -1) { - this.Data.splice(i, 1); - } - } - } else // otherwise, just rebuild the whole list - this.data = this._proxies.map(id => fields[id] as T) - observe(this.Data, () => { - this.updateProxies() - Server.UpdateField(this); - }) + deleted = dataids.indexOf(proxies[i]) != -1; + + this._processingServerUpdate = true; + for (let i = 0; i < proxies.length && added; i++) { + if (dataids.indexOf(proxies[i]) === -1) + this.Data.splice(i, 0, fields[proxies[i]] as T); + } + for (let i = dataids.length - 1; i >= 0 && deleted; i--) { + if (proxies.indexOf(dataids[i]) === -1) + this.Data.splice(i, 1); + } + if (!added && !deleted) {// otherwise, just rebuild the whole list + this.setData(proxies.map(id => fields[id] as T)); + } + this._processingServerUpdate = false; } callback(this); })) -- cgit v1.2.3-70-g09d2