mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00
Enabled bindings to events and gestures for Bindable instead of View.
This commit is contained in:
@ -4,7 +4,9 @@ import helper = require("../helper");
|
||||
import builder = require("ui/builder");
|
||||
import button = require("ui/button");
|
||||
import PageModule = require("ui/page");
|
||||
import viewModule = require("ui/core/view");
|
||||
import fs = require("file-system");
|
||||
import { Observable } from "data/observable";
|
||||
|
||||
// <snippet module="ui/action-bar" title="ActionBar">
|
||||
// # ActionBar
|
||||
@ -172,6 +174,38 @@ export function test_titleView_inXML_short_definition() {
|
||||
TKUnit.assert(centerBtn instanceof button.Button, "cneterView not loaded correctly");
|
||||
};
|
||||
|
||||
export function test_ActionBarItemBindingToEvent() {
|
||||
var p = <PageModule.Page>builder.parse('<Page><Page.actionBar><ActionBar><ActionBar.actionItems><ActionItem tap="{{ test }}"/></ActionBar.actionItems></ActionBar></Page.actionBar></Page>');
|
||||
|
||||
var testAction = function (views: Array<viewModule.View>) {
|
||||
var page = <PageModule.Page>views[0];
|
||||
var firstHandlerCallCounter = 0;
|
||||
var secondHandlerCallCounter = 0;
|
||||
var firstHandler = function () { firstHandlerCallCounter++; };
|
||||
var secondHandler = function () { secondHandlerCallCounter++; };
|
||||
|
||||
page.bindingContext = new Observable({ "test": firstHandler });
|
||||
|
||||
var actionBarItem = page.actionBar.actionItems.getItemAt(0);
|
||||
|
||||
TKUnit.assertEqual((<any>actionBarItem)._observers["tap"].length, 1, "There should be only one listener");
|
||||
TKUnit.assertEqual((<any>actionBarItem)._observers["tap"][0].callback + "", "function () { firstHandlerCallCounter++; }", "First handler is not equal");
|
||||
|
||||
p.bindingContext.set("test", secondHandler);
|
||||
|
||||
TKUnit.assertEqual((<any>actionBarItem)._observers["tap"].length, 1, "There should be only one listener");
|
||||
TKUnit.assertEqual((<any>actionBarItem)._observers["tap"][0].callback + "", "function () { secondHandlerCallCounter++; }", "Second handler is not equal");
|
||||
}
|
||||
|
||||
helper.navigate(function () { return p; });
|
||||
try {
|
||||
testAction([p]);
|
||||
}
|
||||
finally {
|
||||
helper.goBack();
|
||||
}
|
||||
}
|
||||
|
||||
export function test_Setting_ActionItems_doesnt_thrown() {
|
||||
|
||||
var page: PageModule.Page;
|
||||
|
@ -584,9 +584,11 @@ export class Binding {
|
||||
this.updating = true;
|
||||
|
||||
try {
|
||||
if (optionsInstance instanceof viewModule.View &&
|
||||
if (optionsInstance instanceof Bindable &&
|
||||
viewModule.isEventOrGesture(options.property, optionsInstance) &&
|
||||
types.isFunction(value)) {
|
||||
// calling off method with null as handler will remove all handlers for options.property event
|
||||
optionsInstance.off(options.property, null, optionsInstance.bindingContext);
|
||||
optionsInstance.on(options.property, value, optionsInstance.bindingContext);
|
||||
} else {
|
||||
if (optionsInstance instanceof observable.Observable) {
|
||||
|
@ -295,8 +295,10 @@ export class View extends proxy.ProxyObject implements definition.View {
|
||||
|
||||
private _disconnectGestureObservers(type: gestures.GestureTypes): void {
|
||||
var observers = this.getGestureObservers(type);
|
||||
for (let i = 0; i < observers.length; i++) {
|
||||
observers[i].disconnect();
|
||||
if (observers) {
|
||||
for (let i = 0; i < observers.length; i++) {
|
||||
observers[i].disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user