mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00
Binding gestures event handlers support added
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
import view = require("ui/core/view");
|
||||
import builder = require("ui/builder");
|
||||
import page = require("ui/page");
|
||||
import buttonModule = require("ui/button");
|
||||
import switchModule = require("ui/switch");
|
||||
import textFieldModule = require("ui/text-field");
|
||||
import gridLayoutModule = require("ui/layouts/grid-layout");
|
||||
@ -150,6 +151,22 @@ export function test_parse_ShouldParseBindingsWithObservable() {
|
||||
TKUnit.assert(sw.checked === false, "Expected result: false; Actual result: " + sw.checked + "; type: " + typeof (sw.checked));
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBindingsToEvents() {
|
||||
var p = <page.Page>builder.parse("<Page><Button tap='{{ myTap }}' /></Page>");
|
||||
p.bindingContext = { myTap: function (args) { } };
|
||||
var btn = <buttonModule.Button>p.content;
|
||||
|
||||
TKUnit.assert(btn.hasListeners("tap"), "Expected result: true.");
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseBindingsToGestures() {
|
||||
var p = <page.Page>builder.parse("<Page><Label tap='{{ myTap }}' /></Page>");
|
||||
p.bindingContext = { myTap: function (args) { } };
|
||||
var lbl = <labelModule.Label>p.content;
|
||||
|
||||
TKUnit.assert((<any>lbl)._gesturesObserver !== undefined, "Expected result: true.");
|
||||
};
|
||||
|
||||
export function test_parse_ShouldParseSubProperties() {
|
||||
var p = <page.Page>builder.parse("<Page><Switch style.visibility='collapsed' checked='{{ myProp }}' /></Page>");
|
||||
var obj = new observable.Observable();
|
||||
|
@ -109,6 +109,8 @@ export function setPropertyValue(instance: view.View, instanceModule: Object, ex
|
||||
if (isBinding(propertyValue) && instance.bind) {
|
||||
if (isEvent) {
|
||||
attachEventBinding(instance, propertyName, propertyValue);
|
||||
} else if (isGesture(propertyName, instance)) {
|
||||
attachGestureBinding(instance, propertyName, propertyValue);
|
||||
} else {
|
||||
var bindOptions = bindingBuilder.getBindingOptions(propertyName, getBindingExpressionFromAttribute(propertyValue));
|
||||
instance.bind({
|
||||
@ -185,6 +187,22 @@ function attachEventBinding(instance: view.View, eventName: string, value: strin
|
||||
instance.on(observable.Observable.propertyChangeEvent, propertyChangeHandler);
|
||||
}
|
||||
|
||||
function attachGestureBinding(instance: view.View, gestureName: string, value: string) {
|
||||
// Get the event handler from instance.bindingContext.
|
||||
var propertyChangeHandler = (args: observable.PropertyChangeData) => {
|
||||
if (args.propertyName === "bindingContext") {
|
||||
var handler = instance.bindingContext && instance.bindingContext[getBindingExpressionFromAttribute(value)];
|
||||
// Check if the handler is function and add it to the instance for specified event name.
|
||||
if (types.isFunction(handler)) {
|
||||
instance.observe(gestures.fromString(gestureName.toLowerCase()), handler);
|
||||
}
|
||||
instance.off(observable.Observable.propertyChangeEvent, propertyChangeHandler);
|
||||
}
|
||||
};
|
||||
|
||||
instance.on(observable.Observable.propertyChangeEvent, propertyChangeHandler);
|
||||
}
|
||||
|
||||
function isGesture(name: string, instance: any): boolean {
|
||||
return gestures.fromString(name.toLowerCase()) !== undefined;
|
||||
}
|
||||
|
Reference in New Issue
Block a user