Fixed issue with binding gestures to repeater items.

This commit is contained in:
Nedyalko Nikolov
2015-08-14 12:11:41 +03:00
parent 1f8bcc565c
commit 7e6afa603f
5 changed files with 53 additions and 1 deletions

View File

@ -147,6 +147,7 @@
<TypeScriptCompile Include="apps\tests\ui\search-bar\search-bar-tests-native.android.ts" />
<TypeScriptCompile Include="apps\tests\ui\search-bar\search-bar-tests-native.d.ts" />
<TypeScriptCompile Include="apps\tests\ui\search-bar\search-bar-tests-native.ios.ts" />
<TypeScriptCompile Include="apps\tests\ui\repeaterItems-bindingToGestures.ts" />
<TypeScriptCompile Include="apps\tests\xml-declaration\inherited-base-page.ts" />
<TypeScriptCompile Include="apps\tests\xml-declaration\inherited-page.ts" />
<TypeScriptCompile Include="apps\ui-tests-app\pages\handlers.ts" />
@ -898,6 +899,7 @@
<Content Include="apps\tests\pages\page18.xml" />
<Content Include="apps\tests\pages\test2.png" />
<Content Include="apps\tests\ui\bindingContext_testPage.xml" />
<Content Include="apps\tests\ui\repeaterItems-bindingToGestures.xml" />
<Content Include="apps\tests\ui\test-pages\bindingExpressions_logicalComparisonOperators_testPage.xml" />
<Content Include="apps\tests\ui\test-pages\bindingExpressions_comparisonOperators_testPage.xml" />
<Content Include="apps\tests\ui\test-pages\bindingExpressions_binaryOperators_testPage.xml" />

View File

@ -14,6 +14,9 @@ import labelModule = require("ui/label");
import textFieldModule = require("ui/text-field");
import fs = require("file-system");
import appModule = require("application");
import repeaterModule = require("ui/repeater");
import gestureModule = require("ui/gestures");
import layoutBaseModule = require("ui/layouts/layout-base");
// <snippet module="ui/core/bindable" title="bindable">
// For information and examples how to use bindings please refer to special [**Data binding**](../../../../bindings.md) topic.
@ -528,6 +531,30 @@ export var test_BindingContext_NavigatingForwardAndBack = function () {
helper.navigateToModuleAndRunTest(("." + moduleName + "/bindingContext_testPage1"), null, testFunc);
}
export var test_RepeaterItemsGestureBindings = function () {
var testFunc = function (page: pageModule.Page) {
var repeater = <repeaterModule.Repeater>(page.getViewById("repeater"));
var hasObservers = false;
var eachChildCallback = function (childItem: viewModule.View) {
if (childItem instanceof labelModule.Label) {
var gestureObservers = childItem.getGestureObservers(gestureModule.GestureTypes.tap);
hasObservers = gestureObservers ? gestureObservers.length > 0 : false;
}
else if (childItem instanceof layoutBaseModule.LayoutBase) {
childItem._eachChildView(eachChildCallback);
}
return true;
}
repeater._eachChildView(eachChildCallback);
TKUnit.assertEqual(hasObservers, true, "Every item should have tap observer!");
}
var moduleName = __dirname.substr(fs.knownFolders.currentApp().path.length);
helper.navigateToModuleAndRunTest(("." + moduleName + "/repeaterItems-bindingToGestures"), null, testFunc);
}
export var test_BindingToSource_FailsAfterBindingContextChange = function () {
var createLabel = function () {
var label = new labelModule.Label();

View File

@ -0,0 +1,14 @@
function pageLoaded(args) {
var page = args.object;
page.bindingContext = {
items: [
{ text: "1", tapItem: function () { console.log("1") } },
{ text: "2", tapItem: function () { console.log("2") } }
]
};
}
exports.pageLoaded = pageLoaded;
exports.tapPage = function (args) {
console.log("Tap! " + args + " " + args.object);
}

View File

@ -0,0 +1,9 @@
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
<Repeater id="repeater" items="{{ items }}">
<Repeater.itemTemplate>
<GridLayout width="94" height="94" >
<Label text="{{ text }}" tap="{{ tapItem }}" />
</GridLayout>
</Repeater.itemTemplate>
</Repeater>
</Page>

View File

@ -149,8 +149,8 @@ export class Repeater extends viewModule.CustomLayoutView implements definition.
for (i = 0; i < this.items.length; i++) {
var viewToAdd = !types.isNullOrUndefined(this.itemTemplate) ? builder.parse(this.itemTemplate, this) : this._getDefaultItemContent(i);
if (!types.isNullOrUndefined(viewToAdd)) {
this.itemsLayout.addChild(viewToAdd);
viewToAdd.bindingContext = this._getDataItem(i);
this.itemsLayout.addChild(viewToAdd);
}
}
}