UNPKG

type-r2

Version:

Serializable, validated, and observable data layer for modern JS applications

42 lines 1.62 kB
import { __extends } from "tslib"; import { AnyType, ChainableAttributeSpec } from '../record'; import { parseReference } from './commons'; var RecordRefType = (function (_super) { __extends(RecordRefType, _super); function RecordRefType() { return _super !== null && _super.apply(this, arguments) || this; } RecordRefType.prototype.toJSON = function (value) { return value && typeof value === 'object' ? value.id : value; }; RecordRefType.prototype.clone = function (value) { return value && typeof value === 'object' ? value.id : value; }; RecordRefType.prototype.isChanged = function (a, b) { var aId = a && (a.id == null ? a : a.id), bId = b && (b.id == null ? b : b.id); return aId !== bId; }; RecordRefType.prototype.validate = function (model, value, name) { }; return RecordRefType; }(AnyType)); export function memberOf(masterCollection, T) { var getMasterCollection = parseReference(masterCollection); var typeSpec = new ChainableAttributeSpec({ value: null, _metatype: RecordRefType }); return typeSpec .get(function (objOrId, name) { if (typeof objOrId === 'object') return objOrId; var collection = getMasterCollection(this); var record = null; if (collection && collection.length) { record = collection.get(objOrId) || null; this.attributes[name] = record; record && this._attributes[name].handleChange(record, null, this, {}); } return record; }); } //# sourceMappingURL=from.js.map