Skip to content

Commit d8556c4

Browse files
author
Vladimir Enchev
committed
Merge pull request NativeScript#154 from NativeScript/binding-gestures
Binding gestures event handlers support added
2 parents b3f386f + f374fb4 commit d8556c4

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 25 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,30 @@ 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 = {
157+
myTap: function (args) {
158+
//
159+
}
160+
};
161+
var btn = <buttonModule.Button>p.content;
162+
163+
TKUnit.assert(btn.hasListeners("tap"), "Expected result: true.");
164+
};
165+
166+
export function test_parse_ShouldParseBindingsToGestures() {
167+
var p = <page.Page>builder.parse("<Page><Label tap='{{ myTap }}' /></Page>");
168+
p.bindingContext = {
169+
myTap: function (args) {
170+
//
171+
}
172+
};
173+
var lbl = <labelModule.Label>p.content;
174+
175+
TKUnit.assert((<any>lbl)._gesturesObserver !== undefined, "Expected result: true.");
176+
};
177+
153178
export function test_parse_ShouldParseSubProperties() {
154179
var p = <page.Page>builder.parse("<Page><Switch style.visibility='collapsed' checked='{{ myProp }}' /></Page>");
155180
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)