Skip to content

Commit db8edb6

Browse files
Adding in fromJSONData() fct (#131)
* Adding fromJSONData() fct * Update viewbasedjsonmodel.ts Co-authored-by: Kaia Young <[email protected]>
1 parent 4330dd0 commit db8edb6

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/core/viewbasedjsonmodel.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@lumino/algorithm';
88

99
import {
10-
ReadonlyJSONObject, ReadonlyJSONValue
10+
ReadonlyJSONObject, ReadonlyJSONValue, JSONExt
1111
} from '@lumino/coreutils';
1212

1313
import {
@@ -446,6 +446,36 @@ export class ViewBasedJSONModel extends MutableDataModel {
446446
return this.currentView.getSchemaIndex(region, index)
447447
}
448448

449+
/**
450+
* Deep copies data object and mutates it before
451+
* returning a ViewBasedJSONModel of the data.
452+
* ts-ignores are added since the properties to be mutated
453+
* are readonly
454+
*
455+
* @param data - Data passed in to be transformed
456+
*
457+
*/
458+
static fromJsonData(data: ViewBasedJSONModel.IData): ViewBasedJSONModel {
459+
const primaryKeyUuid = "ipydguuid";
460+
461+
let newData = <ViewBasedJSONModel.IData><unknown>JSONExt.deepCopy(<ReadonlyJSONObject><unknown>data);
462+
463+
//@ts-ignore
464+
newData.schema.primaryKeyUuid = primaryKeyUuid;
465+
466+
for (let field of newData.schema.fields) {
467+
//@ts-ignore
468+
field.rows = [field.name];
469+
}
470+
let count = 0;
471+
for (let row of newData.data) {
472+
//@ts-ignore
473+
row[primaryKeyUuid] = count++;
474+
}
475+
476+
return new ViewBasedJSONModel(newData);
477+
}
478+
449479
private _currentView: View;
450480
private _transformSignal = new Signal<this, TransformStateManager.IEvent>(this);
451481
private _dataSyncSignal = new Signal<this, ViewBasedJSONModel.IDataSyncEvent>(this);
@@ -588,4 +618,4 @@ namespace ViewBasedJSONModel {
588618
*/
589619
indices: number[];
590620
}
591-
}
621+
}

0 commit comments

Comments
 (0)