本文整理汇总了TypeScript中@orbit/data.cloneRecordIdentity函数的典型用法代码示例。如果您正苦于以下问题:TypeScript cloneRecordIdentity函数的具体用法?TypeScript cloneRecordIdentity怎么用?TypeScript cloneRecordIdentity使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cloneRecordIdentity函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test
test('remove record with hasMany relationships', function(assert) {
const earth = { type: 'planet', id: 'earth', relationships: { inhabitants: { data: [{ type: 'inhabitant', id: 'human' }] } } };
const human = { type: 'inhabitant', id: 'human', relationships: { planets: { data: [{ type: 'planet', id: 'earth' }] } } };
cache.patch(t => [
t.addRecord(earth),
t.addRecord(human)
]);
assert.deepEqual(cache.inverseRelationships.all(earth), [{
record: { type: 'inhabitant', id: 'human' },
relationship: 'planets'
}]);
assert.deepEqual(cache.inverseRelationships.all(human), [{
record: { type: 'planet', id: 'earth' },
relationship: 'inhabitants'
}]);
const removeInhabitantOp = {
op: 'removeRecord',
record: human
};
assert.deepEqual(
processor.before(removeInhabitantOp),
[]
);
assert.deepEqual(
processor.after(removeInhabitantOp),
[
{
op: 'removeFromRelatedRecords',
record: cloneRecordIdentity(earth),
relationship: 'inhabitants',
relatedRecord: cloneRecordIdentity(human)
}
]
);
assert.deepEqual(
processor.finally(removeInhabitantOp),
[]
);
assert.deepEqual(cache.inverseRelationships.all(earth), []);
assert.deepEqual(cache.inverseRelationships.all(human), []);
});
示例2: getAllInverseRelationships
export function getAllInverseRelationships(
record: Record
): RecordRelationshipIdentity[] {
const relationships = record && record.relationships;
if (relationships) {
const recordIdentity = cloneRecordIdentity(record);
const inverseRelationships: RecordRelationshipIdentity[] = [];
Object.keys(relationships).forEach(relationship => {
const relationshipData =
relationships[relationship] && relationships[relationship].data;
if (relationshipData) {
if (isArray(relationshipData)) {
const relatedRecords = relationshipData as Record[];
relatedRecords.forEach(relatedRecord => {
inverseRelationships.push({
record: recordIdentity,
relationship,
relatedRecord
});
});
} else {
const relatedRecord = relationshipData as Record;
inverseRelationships.push({
record: recordIdentity,
relationship,
relatedRecord
});
}
}
});
return inverseRelationships;
}
}
示例3: clearInverseRelationshipOps
private clearInverseRelationshipOps(record: RecordIdentity): RecordOperation[] {
const ops: RecordOperation[] = [];
const inverseRels = this.cache.inverseRelationships.all(record);
if (inverseRels.length > 0) {
const recordIdentity = cloneRecordIdentity(record);
inverseRels.forEach(rel => {
const relationshipDef = this.cache.schema.getModel(rel.record.type).relationships[rel.relationship];
if (relationshipDef.type === 'hasMany') {
ops.push({
op: 'removeFromRelatedRecords',
record: rel.record,
relationship: rel.relationship,
relatedRecord: recordIdentity
});
} else {
ops.push({
op: 'replaceRelatedRecord',
record: rel.record,
relationship: rel.relationship,
relatedRecord: null
});
}
});
}
return ops;
}
示例4: recordRemoved
export function recordRemoved(
schema: Schema,
record: Record
): RecordOperation[] {
const ops: RecordOperation[] = [];
const relationships = record && record.relationships;
if (relationships) {
const modelDef = schema.getModel(record.type);
const recordIdentity = cloneRecordIdentity(record);
Object.keys(relationships).forEach(relationship => {
const relationshipDef = modelDef.relationships[relationship];
const relationshipData =
relationships[relationship] && relationships[relationship].data;
const relatedRecords = recordArrayFromData(relationshipData);
Array.prototype.push.apply(
ops,
removeRelatedRecordsOps(
schema,
recordIdentity,
relationshipDef,
relatedRecords
)
);
});
}
return ops;
}
示例5: test
test('replace hasMany => hasMany, clearing records', function(assert) {
const human = {
type: 'inhabitant',
id: 'human',
relationships: { planets: { data: [{ type: 'planet', id: 'earth' }] } }
};
const earth = {
type: 'planet',
id: 'earth',
relationships: {
inhabitants: { data: [{ type: 'inhabitant', id: 'human' }] }
}
};
cache.patch(t => [t.addRecord(earth), t.addRecord(human)]);
const clearInhabitantsOp: ReplaceRelatedRecordsOperation = {
op: 'replaceRelatedRecords',
record: identity(earth),
relationship: 'inhabitants',
relatedRecords: []
};
assert.deepEqual(processor.after(clearInhabitantsOp), [
{
op: 'removeFromRelatedRecords',
record: identity(human),
relationship: 'planets',
relatedRecord: identity(earth)
}
]);
assert.deepEqual(processor.finally(clearInhabitantsOp), []);
});
示例6: test
test('replace hasMany => hasOne with populated array, when already populated', function(assert) {
const saturn = { type: 'planet', id: 'saturn',
attributes: { name: 'Saturn' },
relationships: { moons: { data: [{ type: 'moon', id: 'titan' }] } } };
const jupiter = { type: 'planet', id: 'jupiter',
attributes: { name: 'Jupiter' },
relationships: { moons: { data: [{ type: 'moon', id: 'europa' }] } } };
const titan = { type: 'moon', id: 'titan',
attributes: { name: 'Titan' },
relationships: { planet: { data: { type: 'planet', id: 'saturn' } } } };
const europa = { type: 'moon', id: 'europa',
attributes: { name: 'Europa' },
relationships: { planet: { data: { type: 'planet', id: 'jupiter' } } } };
cache.patch(t => [
t.addRecord(saturn),
t.addRecord(jupiter),
t.addRecord(titan),
t.addRecord(europa)
]);
const replaceMoonsOp = {
op: 'replaceRelatedRecords',
record: identity(saturn),
relationship: 'moons',
relatedRecords: [identity(europa)]
};
assert.deepEqual(
processor.before(replaceMoonsOp),
[]
);
assert.deepEqual(
processor.after(replaceMoonsOp),
[
{
op: 'replaceRelatedRecord',
record: identity(titan),
relationship: 'planet',
relatedRecord: null
},
{
op: 'replaceRelatedRecord',
record: identity(europa),
relationship: 'planet',
relatedRecord: identity(saturn)
}
]
);
assert.deepEqual(
processor.finally(replaceMoonsOp),
[]
);
});
示例7: identity
.then(() => {
assert.deepEqual(
rollbackOperations,
[
{ op: 'removeRecord', record: identity(recordE) },
{ op: 'removeRecord', record: identity(recordD) },
{ op: 'removeRecord', record: identity(recordC) },
{ op: 'removeRecord', record: identity(recordB) }
],
'emits inverse operations in correct order'
);
assert.equal(store.transformLog.head, addRecordATransform.id, 'rolls back transform log');
});