Skip to content

Commit fcc2596

Browse files
committed
Scenario #9 completed.
1 parent d9344d2 commit fcc2596

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!-- #docregion -->
2+
<button [hidden]="!winner || winner == hero"
3+
(click)='stealJob()'>
4+
I steal the job!
5+
</button>
6+
<!-- #enddocregion -->

public/docs/_examples/component-communication/ts/steal-job/src/app/invited-hero.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ export class InvitedHero {
113113
? "I won the job !!!"
114114
: "I lost the job :-(";
115115
}
116-
116+
// #docregion steal
117117
stealJob() {
118118
this.undertaken = true;
119119
this.jobService.assign(this.hero);
120120
}
121+
// #enddocregion steal
121122
}

public/docs/ts/latest/guide/component-communication.jade

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,51 @@ figure.image-display
544544

545545
## #8: A malicious hero steals the job &mdash; issues with multiple facades
546546

547-
_Content_
547+
With moving the business logic to `JobService`, we made a great design decision, as we can manage business and UI logic separately. However the _way we use_
548+
`JobService` has a flaw. Right now both `HeroJobBoard` and `InvitedHero` use a single facade to access `JobService` functionality, and through this facade
549+
these components can access all operations, and not only the ones intended for their use. The following blueprint shows how the members of `JobService` are used:
550+
551+
code-example(format='linenums', language='typescript').
552+
export class JobService {
553+
get jobRequest(); // HeroJobBoard
554+
get respondingHeroes(); // HeroJobBoard
555+
get assignedTo(); // HeroJobBoard
556+
557+
jobPostEvent; // HeroJobBoard and InvitedHero
558+
jobAssignedEvent; // HeroJobBoard and InvitedHero
559+
560+
post(jobRequest: string); // HeroJobBoard
561+
take(hero: Hero); // InvitedHero
562+
assign(hero: Hero); // HeroJobBoard
563+
}
564+
565+
:marked
566+
This design allows that components can invoke operations that were not available for them with properly designed facades. We'll it by adding a new `stealJob()` method
567+
to `InvitedHero` so that a hero can maliciously steal a job assigned to another hero:
568+
569+
+makeExample('component-communication/ts/steal-job/src/app/invited-hero.ts', 'steal', 'invited-hero.ts')
570+
571+
:marked
572+
Let's append this markup snippet to the template of `InvitedHero` so that we can invoke `stealJob()` from the UI:
548573

574+
+makeExample('component-communication/ts/steal-job/fragments/steal.html', null, 'invited-hero.ts (template)')
575+
576+
:marked
577+
Now, an `InvitedHero` instance can behave as if it were `HeroJobBoard` and invoke the `assign()` method of `JobService`, passing itself as the winner hero.
578+
579+
When we run the application, after the job is assigned to a hero, all other heroes (even those who have not undertaken the job before) can click the **I steal the job!**
580+
button and scrounge the job. This screenshot shows that Magneta has been assigned the job:
581+
582+
figure.image-display
583+
img(src="/resources/images/devguide/component-communication/winner-announced-ui.png" alt="The winner is announced")
584+
585+
:marked
586+
Now, Bombasto can use the **I steal the job button!**, and the job goes to him. (It's not a great consolation for Magneta that she can steal the job back...)
587+
588+
figure.image-display
589+
img(src="/resources/images/devguide/component-communication/job-stolen-ui.png" alt="The job is stolen")
590+
591+
:marked
549592
## #9: Preventing stealth &mdash; using multiple facades
550593

551594
_Content_
49.4 KB
Loading
47.3 KB
Loading

0 commit comments

Comments
 (0)