mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
BindingContext set two times on adding item to ListView. Fixes issue #410.
This commit is contained in:
@ -580,6 +580,33 @@ export function test_bindingToParentObjectWithSpacesInIndexer() {
|
|||||||
helper.buildUIAndRunTest(listView, testAction);
|
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() {
|
export function test_no_memory_leak_when_items_is_regular_array() {
|
||||||
var createFunc = function (): listViewModule.ListView {
|
var createFunc = function (): listViewModule.ListView {
|
||||||
var listView = new listViewModule.ListView();
|
var listView = new listViewModule.ListView();
|
||||||
|
@ -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"
|
* Method is intended to be overridden by inheritors and used as "protected"
|
||||||
*/
|
*/
|
||||||
public _addViewCore(view: View) {
|
public _addViewCore(view: View) {
|
||||||
view._setValue(bindable.Bindable.bindingContextProperty, this.bindingContext, dependencyObservable.ValueSource.Inherited);
|
this._propagateInheritableProperties(view);
|
||||||
|
|
||||||
view._inheritProperties(this);
|
|
||||||
|
|
||||||
view.style._inheritStyleProperties();
|
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 that = this;
|
||||||
var inheritablePropertySetCallback = function (property: dependencyObservable.Property) {
|
var inheritablePropertySetCallback = function (property: dependencyObservable.Property) {
|
||||||
if (property instanceof styling.Property) {
|
if (property instanceof styling.Property) {
|
||||||
|
2
ui/core/view.d.ts
vendored
2
ui/core/view.d.ts
vendored
@ -409,6 +409,8 @@ declare module "ui/core/view" {
|
|||||||
isLoaded: boolean;
|
isLoaded: boolean;
|
||||||
|
|
||||||
_addView(view: View);
|
_addView(view: View);
|
||||||
|
_propagateInheritableProperties(view: View)
|
||||||
|
_inheritProperties(parentView: View)
|
||||||
_removeView(view: View);
|
_removeView(view: View);
|
||||||
_context: android.content.Context;
|
_context: android.content.Context;
|
||||||
|
|
||||||
|
@ -114,6 +114,7 @@ export class ListView extends view.View implements definition.ListView {
|
|||||||
public _prepareItem(item: view.View, index: number) {
|
public _prepareItem(item: view.View, index: number) {
|
||||||
if (item) {
|
if (item) {
|
||||||
item.bindingContext = this._getDataItem(index);
|
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) {
|
private _onItemsChanged(args: observable.EventData) {
|
||||||
this.refresh();
|
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.
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user