-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ca65c0
commit af2c586
Showing
13 changed files
with
259 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<c8y-title>Sharing a service between plugins</c8y-title> | ||
<div class="card-block"> | ||
<p> | ||
A service instance can be used by components that need to communicate to each other or share a | ||
common state. If these two components originate from different code bases, e.g. are deployed via | ||
plugin-ins, then | ||
<code>hookService</code> | ||
comes as a way for such a shared service to be injected. The service interface might be declared | ||
in a shared library known at compile time. | ||
</p> | ||
<p> | ||
This example showcases two component instances that share a counter service. The counter service | ||
interface is declared in the | ||
<code>counder.model</code> | ||
module which may be declared in a common library shared between plug-ins in a real world case. | ||
The | ||
<code>CounterHookModule</code> | ||
leverages | ||
<code>hookService</code> | ||
to inject a service instance. This can happen in a plugin or in a shell application. | ||
</p> | ||
</div> | ||
<div class="card-group"> | ||
<div class="col-md-6"> | ||
<div class="card"> | ||
<div class="card-header separator"> | ||
<p class="card-title">Component A</p> | ||
</div> | ||
<div class="card-block"> | ||
<c8y-dynamic-component componentId="counter.component"></c8y-dynamic-component> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="col-md-6"> | ||
<div class="card"> | ||
<div class="card-header separator"> | ||
<p class="card-title">Component B</p> | ||
</div> | ||
<div class="card-block"> | ||
<c8y-dynamic-component componentId="counter.component"></c8y-dynamic-component> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Component } from '@angular/core'; | ||
import { CoreModule } from '@c8y/ngx-components'; | ||
|
||
/** | ||
* This is the component that hosts the Service demo view. | ||
*/ | ||
@Component({ | ||
selector: 'tut-basic-component-hook-view', | ||
templateUrl: './basic-view.component.html', | ||
standalone: true, | ||
imports: [CoreModule] | ||
}) | ||
export class BasicViewComponent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<p> | ||
Hello there! I am a simple component added from a plugin by | ||
<code>hookComponent</code> | ||
. I use a shared counter service that has been provided in another plugin and injected by | ||
<code>hookService</code> | ||
. | ||
</p> | ||
<button | ||
class="btn btn-default m-t-16" | ||
type="button" | ||
(click)="counter.count()" | ||
> | ||
Click to increment counter | ||
<span class="badge badge-system">{{ counter.counter }}</span> | ||
</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Component } from '@angular/core'; | ||
import { ServiceRegistry } from '@c8y/ngx-components'; | ||
import { ICounterService } from '../counter/counter.model'; | ||
|
||
@Component({ | ||
selector: 'tut-counter-component', | ||
templateUrl: './counter.component.html', | ||
standalone: true | ||
}) | ||
export class CounterComponent { | ||
counter: ICounterService; | ||
|
||
constructor(registry: ServiceRegistry) { | ||
/** | ||
* To retrieve an instance of a service injected by `hookService` you can use ServiceRegistry.get(key) method. | ||
* It will return all injected service instances in a type-safe manner if there is a typed extension key declared. | ||
* For an example of such declaration check the declarations in `counter/counter.model.ts`. | ||
*/ | ||
this.counter = registry.get('counter').at(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './basic-view.component'; | ||
export * from './counter.component'; | ||
export * from './service-hook.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { NavigatorNode, hookComponent, hookNavigator, hookRoute } from '@c8y/ngx-components'; | ||
|
||
@NgModule({ | ||
providers: [ | ||
/* Hook the Service hook demo view */ | ||
hookRoute({ | ||
path: 'hooks/service', | ||
loadComponent: () => import('./basic-view.component').then(m => m.BasicViewComponent) | ||
}), | ||
hookNavigator( | ||
new NavigatorNode({ | ||
priority: 40, | ||
path: 'hooks/service', | ||
icon: 'gears', | ||
label: 'Service', | ||
parent: 'Hooks' | ||
}) | ||
), | ||
/* Hook a client component for the service provided via `hookService` */ | ||
hookComponent({ | ||
id: 'counter.component', | ||
label: 'Counter component', | ||
description: 'This component can count', | ||
loadComponent: () => import('./counter.component').then(m => m.CounterComponent) | ||
}) | ||
] | ||
}) | ||
export class ServiceHookModule {} |
Oops, something went wrong.