Skip to content

Commit 9e7fbc8

Browse files
author
Vladimir Enchev
committed
Binding gestures event handlers support added
1 parent b3f386f commit 9e7fbc8

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

apps/tests/xml-declaration/xml-declaration-tests.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import view = require("ui/core/view");
33
import builder = require("ui/builder");
44
import page = require("ui/page");
5+
import buttonModule = require("ui/button");
56
import switchModule = require("ui/switch");
67
import textFieldModule = require("ui/text-field");
78
import gridLayoutModule = require("ui/layouts/grid-layout");
@@ -150,6 +151,22 @@ export function test_parse_ShouldParseBindingsWithObservable() {
150151
TKUnit.assert(sw.checked === false, "Expected result: false; Actual result: " + sw.checked + "; type: " + typeof (sw.checked));
151152
};
152153

154+
export function test_parse_ShouldParseBindingsToEvents() {
155+
var p = <page.Page>builder.parse("<Page><Button tap='{{ myTap }}' /></Page>");
156+
p.bindingContext = { myTap: function (args) { } };
157+
var btn = <buttonModule.Button>p.content;
158+
159+
TKUnit.assert(btn.hasListeners("tap"), "Expected result: true.");
160+
};
161+
162+
export function test_parse_ShouldParseBindingsToGestures() {
163+
var p = <page.Page>builder.parse("<Page><Label tap='{{ myTap }}' /></Page>");
164+
p.bindingContext = { myTap: function (args) { } };
165+
var lbl = <labelModule.Label>p.content;
166+
167+
TKUnit.assert((<any>lbl)._gesturesObserver !== undefined, "Expected result: true.");
168+
};
169+
153170
export function test_parse_ShouldParseSubProperties() {
154171
var p = <page.Page>builder.parse("<Page><Switch style.visibility='collapsed' checked='{{ myProp }}' /></Page>");
155172
var obj = new observable.Observable();

ui/builder/component-builder.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ export function setPropertyValue(instance: view.View, instanceModule: Object, ex
109109
if (isBinding(propertyValue) && instance.bind) {
110110
if (isEvent) {
111111
attachEventBinding(instance, propertyName, propertyValue);
112+
} else if (isGesture(propertyName, instance)) {
113+
attachGestureBinding(instance, propertyName, propertyValue);
112114
} else {
113115
var bindOptions = bindingBuilder.getBindingOptions(propertyName, getBindingExpressionFromAttribute(propertyValue));
114116
instance.bind({
@@ -185,6 +187,22 @@ function attachEventBinding(instance: view.View, eventName: string, value: strin
185187
instance.on(observable.Observable.propertyChangeEvent, propertyChangeHandler);
186188
}
187189

190+
function attachGestureBinding(instance: view.View, gestureName: string, value: string) {
191+
// Get the event handler from instance.bindingContext.
192+
var propertyChangeHandler = (args: observable.PropertyChangeData) => {
193+
if (args.propertyName === "bindingContext") {
194+
var handler = instance.bindingContext && instance.bindingContext[getBindingExpressionFromAttribute(value)];
195+
// Check if the handler is function and add it to the instance for specified event name.
196+
if (types.isFunction(handler)) {
197+
instance.observe(gestures.fromString(gestureName.toLowerCase()), handler);
198+
}
199+
instance.off(observable.Observable.propertyChangeEvent, propertyChangeHandler);
200+
}
201+
};
202+
203+
instance.on(observable.Observable.propertyChangeEvent, propertyChangeHandler);
204+
}
205+
188206
function isGesture(name: string, instance: any): boolean {
189207
return gestures.fromString(name.toLowerCase()) !== undefined;
190208
}

0 commit comments

Comments
 (0)