Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public update(data: PureModel | IDictionary): object {
deprecated('model.update is deprecated. Use updateModel() instead.');
const updateData = Object.assign({ }, data);
Object.keys(updateData).forEach((key) => {
if (typeof this[key] === 'function') {
// tslint:disable-next-line:no-dynamic-delete
delete updateData[key];
}
});
return updateModel(this, updateData);
}
constructor(initialData: object, collection?: Collection) {
super(initialData, collection);
deprecated('CompatModel is just a migration tool. Please move to Model or PureModel as soon as possible.');
Object.keys(this.static.refs).forEach((prop) => {
const refs = getModelMetaKey(this, 'refs');
if (prop in refs) {
return;
}
const ref = this.static.refs[prop];
const data = mapItems(this[prop] || this.static.defaults[prop], getModelId);
assignComputed(this, prop);
if (typeof ref === 'object') {
// Back reference
initModelRef(this, prop, {
model: ref.model,
property: ref.property,
type: ReferenceType.TO_ONE_OR_MANY,
}, data);
type: FieldType = FieldType.DATA,
) {
const fields = getModelMetaKey(obj, 'fields') as Array;
if (type === FieldType.ID && key in obj) {
storage.setModelDataKey(obj, key, undefined);
}
// Initialize the observable field to the default value
storage.setModelDataKey(obj, key, defValue);
updateAction(obj, key, defValue);
if (fields.indexOf(key) === -1) {
fields.push(key);
}
assignComputed(obj, key,
() => getField(obj, key),
(value) => {
updateField(obj, key, value, type);
},
);
}
export function initModelRef(obj: PureModel, key: string, options: IReferenceOptions, initialVal: TRefValue) {
const refs = getModelMetaKey(obj, 'refs');
// Initialize the observable field to the given value
refs[key] = options;
const isArray = options.type === ReferenceType.TO_MANY;
storage.setModelDataKey(obj, key, isArray ? [] : undefined);
updateAction(obj, key, isArray ? [] : undefined);
assignComputed(obj, key,
() => getRef(obj, key),
(value) => {
updateRef(obj, key, value);
},
);
if (!options.property && initialVal !== undefined) {
obj[key] = initialVal;
}
}
Object.keys(this.static.refs).forEach((prop) => {
const refs = getModelMetaKey(this, 'refs');
if (prop in refs) {
return;
}
const ref = this.static.refs[prop];
const data = mapItems(this[prop] || this.static.defaults[prop], getModelId);
assignComputed(this, prop);
if (typeof ref === 'object') {
// Back reference
initModelRef(this, prop, {
model: ref.model,
property: ref.property,
type: ReferenceType.TO_ONE_OR_MANY,
}, data);
} else {
// Normal reference
initModelRef(this, prop, {
model: ref,
type: ReferenceType.TO_ONE_OR_MANY,
}, data);
}
});
Object.keys(this.static.refs).forEach((prop) => {
const refs = getModelMetaKey(this, 'refs');
if (prop in refs) {
return;
}
const ref = this.static.refs[prop];
const data = mapItems(this[prop] || this.static.defaults[prop], getModelId);
assignComputed(this, prop);
if (typeof ref === 'object') {
// Back reference
initModelRef(this, prop, {
model: ref.model,
property: ref.property,
type: ReferenceType.TO_ONE_OR_MANY,
}, data);
} else {
// Normal reference
initModelRef(this, prop, {
model: ref,
type: ReferenceType.TO_ONE_OR_MANY,
}, data);
}
});
public get static(): typeof CompatCollection {
deprecated('collection.static is deprecated.');
return this.constructor as typeof CompatCollection;
}
public toJS() {
deprecated('model.toJS() is deprecated. Use modelToJSON() instead.');
return modelToJSON(this);
}
private get __collection() {
deprecated('model.__collection is deprecated. Use getModelCollection() instead.');
return getModelCollection(this);
}
}
public assignRef(key: string, value: any, type?: IType) {
deprecated('model.assignRef is deprecated. Use initModelRef() instead.');
const refs = getModelMetaKey(this, 'refs');
if (refs[key]) {
return this[key] = value;
}
let model = type;
if (!model) {
if (value instanceof Array || isObservableArray(value)) {
model = value.reduce((t, m) => t || getModelType(m), null);
} else if (value instanceof PureModel) {
model = getModelType(value);
}
}
if (!model) {