1 | import { bindingMode, EventSubscriber, bindingBehavior } from 'aurelia-binding';
|
2 |
|
3 |
|
4 | const eventNamesRequired = 'The updateTrigger binding behavior requires at least one event name argument: eg <input value.bind="firstName & updateTrigger:\'blur\'">';
|
5 | const notApplicableMessage = 'The updateTrigger binding behavior can only be applied to two-way/ from-view bindings on input/select elements.';
|
6 |
|
7 | @bindingBehavior('updateTrigger')
|
8 | export class UpdateTriggerBindingBehavior {
|
9 |
|
10 | bind(binding, source, ...events) {
|
11 | if (events.length === 0) {
|
12 | throw new Error(eventNamesRequired);
|
13 | }
|
14 | if (binding.mode !== bindingMode.twoWay && binding.mode !== bindingMode.fromView) {
|
15 | throw new Error(notApplicableMessage);
|
16 | }
|
17 |
|
18 |
|
19 | let targetObserver = binding.observerLocator.getObserver(binding.target, binding.targetProperty);
|
20 | if (!targetObserver.handler) {
|
21 | throw new Error(notApplicableMessage);
|
22 | }
|
23 | binding.targetObserver = targetObserver;
|
24 |
|
25 |
|
26 | targetObserver.originalHandler = binding.targetObserver.handler;
|
27 |
|
28 |
|
29 | let handler = new EventSubscriber(events);
|
30 | targetObserver.handler = handler;
|
31 | }
|
32 |
|
33 |
|
34 | unbind(binding, source) {
|
35 | let targetObserver = binding.targetObserver;
|
36 |
|
37 | targetObserver.handler.dispose();
|
38 | targetObserver.handler = targetObserver.originalHandler;
|
39 | targetObserver.originalHandler = null;
|
40 | }
|
41 | }
|