-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
update-trigger-binding-behavior.ts
39 lines (32 loc) · 1.58 KB
/
update-trigger-binding-behavior.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { bindingMode, EventSubscriber, bindingBehavior } from 'aurelia-binding';
// tslint:disable-next-line:max-line-length
const eventNamesRequired = 'The updateTrigger binding behavior requires at least one event name argument: eg <input value.bind="firstName & updateTrigger:\'blur\'">';
const notApplicableMessage = 'The updateTrigger binding behavior can only be applied to two-way/ from-view bindings on input/select elements.';
@bindingBehavior('updateTrigger')
export class UpdateTriggerBindingBehavior {
bind(binding, source, ...events) {
if (events.length === 0) {
throw new Error(eventNamesRequired);
}
if (binding.mode !== bindingMode.twoWay && binding.mode !== bindingMode.fromView) {
throw new Error(notApplicableMessage);
}
// ensure the binding's target observer has been set.
let targetObserver = binding.observerLocator.getObserver(binding.target, binding.targetProperty);
if (!targetObserver.handler) {
throw new Error(notApplicableMessage);
}
binding.targetObserver = targetObserver;
// stash the original element subscribe function.
targetObserver.originalHandler = binding.targetObserver.handler;
// replace the element subscribe function with one that uses the correct events.
let handler = new EventSubscriber(events);
targetObserver.handler = handler;
}
unbind(binding, source) {
// restore the state of the binding.
binding.targetObserver.handler.dispose();
binding.targetObserver.handler = binding.targetObserver.originalHandler;
binding.targetObserver.originalHandler = null;
}
}