Skip to content

Custom Model Example

This example demonstrates how to create a custom model implementation that persists data directly to localStorage, providing automatic persistence without keeping local copies in memory.


import '@angular/compiler';
import { Component } from '@angular/core';
import { provideNgDiagram } from 'ng-diagram';
import { DiagramComponent } from './diagram.component';
@Component({
imports: [DiagramComponent],
providers: [provideNgDiagram()],
template: `<diagram />`,
})
export class DiagramWrapperComponent {}

The NgDiagramComponent accepts any object, via the model input property (e.g. [model]="customModelAdapter"), that implements the ModelAdapter interface. This means you can create your own custom model implementations beyond the default SignalModelAdapter provided by initializeModel. This allows for advanced use cases like connecting to external data sources, implementing custom persistence layers, or integrating with existing state management solutions.

  • Direct localStorage Persistence: All data is read from and written directly to localStorage
  • Single Source of Truth: No local copies of data are maintained in memory
  • Automatic Synchronization: Changes are immediately persisted
  • Error Handling: Robust error handling for storage operations

The ModelAdapter interface defines the contract that any model implementation must fulfill. You can find the complete interface documentation in the API reference. The key methods include:

This example demonstrates a custom model adapter that persists diagram data to localStorage. The data will survive page refreshes and browser sessions. You can try it by adding nodes and edges, then refreshing the page to see that your changes are retained.