BindingContext set two times on adding item to ListView. Fixes issue #410.

This commit is contained in:
Nedyalko Nikolov
2015-07-14 14:47:40 +03:00
parent feadf177de
commit b45718b1ef
4 changed files with 40 additions and 4 deletions

View File

@ -580,6 +580,33 @@ export function test_bindingToParentObjectWithSpacesInIndexer() {
helper.buildUIAndRunTest(listView, testAction);
}
export function test_ConverterIsCalledJustOnce_onAddingItemsToListView() {
var listView = new listViewModule.ListView();
var converterCalledCounter = 0;
var testConverter = function (value) {
converterCalledCounter++;
return value;
}
app.resources["testConverter"] = testConverter;
function testAction(views: Array<viewModule.View>) {
var listViewModel = new observable.Observable();
listViewModel.set("items", [1, 2, 3]);
listView.bindingContext = listViewModel;
listView.bind({ sourceProperty: "items", targetProperty: "items" });
listView.itemTemplate = "<Label id=\"testLabel\" text=\"{{ $value, $value | testConverter }}\" />";
TKUnit.wait(ASYNC);
TKUnit.assertEqual(converterCalledCounter, listViewModel.get("items").length, "Converter should be called once for every item.");
}
helper.buildUIAndRunTest(listView, testAction);
}
export function test_no_memory_leak_when_items_is_regular_array() {
var createFunc = function (): listViewModule.ListView {
var listView = new listViewModule.ListView();

View File

@ -829,9 +829,7 @@ export class View extends proxy.ProxyObject implements definition.View {
* Method is intended to be overridden by inheritors and used as "protected"
*/
public _addViewCore(view: View) {
view._setValue(bindable.Bindable.bindingContextProperty, this.bindingContext, dependencyObservable.ValueSource.Inherited);
view._inheritProperties(this);
this._propagateInheritableProperties(view);
view.style._inheritStyleProperties();
@ -845,7 +843,11 @@ export class View extends proxy.ProxyObject implements definition.View {
}
}
private _inheritProperties(parentView: View) {
public _propagateInheritableProperties(view: View) {
view._inheritProperties(this);
}
public _inheritProperties(parentView: View) {
var that = this;
var inheritablePropertySetCallback = function (property: dependencyObservable.Property) {
if (property instanceof styling.Property) {

2
ui/core/view.d.ts vendored
View File

@ -409,6 +409,8 @@ declare module "ui/core/view" {
isLoaded: boolean;
_addView(view: View);
_propagateInheritableProperties(view: View)
_inheritProperties(parentView: View)
_removeView(view: View);
_context: android.content.Context;

View File

@ -114,6 +114,7 @@ export class ListView extends view.View implements definition.ListView {
public _prepareItem(item: view.View, index: number) {
if (item) {
item.bindingContext = this._getDataItem(index);
item._inheritProperties(this);
}
}
@ -145,4 +146,8 @@ export class ListView extends view.View implements definition.ListView {
private _onItemsChanged(args: observable.EventData) {
this.refresh();
}
public _propagateInheritableProperties(view: view.View) {
// do not get binding context from parent when adding items, since the binding context of the items will be different.
}
}