Made TabViewItem bindable.

This commit is contained in:
Rossen Hristov
2015-08-28 12:43:16 +03:00
parent db8b1172f9
commit 881c378a4e
7 changed files with 84 additions and 25 deletions

View File

@@ -35,7 +35,14 @@ export class Bindable extends dependencyObservable.DependencyObservable implemen
public static bindingContextProperty = bindingContextProperty;
// TODO: Implement with WeakRef to prevent memory leaks.
private _bindings = {};
private _bindings: Object;
private get bindings(): Object {
if (!this._bindings) {
this._bindings = {};
}
return this._bindings;
}
get bindingContext(): Object {
return this._getValue(Bindable.bindingContextProperty);
@@ -45,13 +52,13 @@ export class Bindable extends dependencyObservable.DependencyObservable implemen
}
public bind(options: definition.BindingOptions, source?: Object) {
var binding: Binding = this._bindings[options.targetProperty];
var binding: Binding = this.bindings[options.targetProperty];
if (binding) {
binding.unbind();
}
binding = new Binding(this, options);
this._bindings[options.targetProperty] = binding;
this.bindings[options.targetProperty] = binding;
var bindingSource = source;
if (!bindingSource) {
@@ -64,15 +71,15 @@ export class Bindable extends dependencyObservable.DependencyObservable implemen
}
public unbind(property: string) {
var binding: Binding = this._bindings[property];
var binding: Binding = this.bindings[property];
if (binding) {
binding.unbind();
delete this._bindings[property];
delete this.bindings[property];
}
}
public _updateTwoWayBinding(propertyName: string, value: any) {
var binding: Binding = this._bindings[propertyName];
var binding: Binding = this.bindings[propertyName];
if (binding) {
binding.updateTwoWay(value);
@@ -92,7 +99,7 @@ export class Bindable extends dependencyObservable.DependencyObservable implemen
return;
}
}
var binding = this._bindings[property.name];
var binding = this.bindings[property.name];
if (binding && !binding.updating) {
if (binding.options.twoWay) {
trace.write("_updateTwoWayBinding(" + this + "): " + property.name, trace.categories.Binding);
@@ -107,8 +114,8 @@ export class Bindable extends dependencyObservable.DependencyObservable implemen
public _onBindingContextChanged(oldValue: any, newValue: any) {
var binding: Binding;
for (var p in this._bindings) {
binding = this._bindings[p];
for (var p in this.bindings) {
binding = this.bindings[p];
if (binding.updating || !binding.sourceIsBindingContext) {
continue;

View File

@@ -1,12 +1,53 @@
import definition = require("ui/tab-view");
import view = require("ui/core/view");
import observable = require("data/observable");
import dependencyObservable = require("ui/core/dependency-observable");
import proxy = require("ui/core/proxy");
import types = require("utils/types");
import trace = require("trace");
import bindable = require("ui/core/bindable");
export var traceCategory = "TabView";
export class TabViewItem extends bindable.Bindable implements definition.TabViewItem {
private _title: string;
private _view: view.View;
private _iconSource: string;
get title(): string {
return this._title;
}
set title(value: string) {
if (this._title !== value) {
this._title = value;
console.log(`set this._title = ${value};`)
}
}
get view(): view.View {
return this._view;
}
set view(value: view.View) {
if (this._view !== value) {
this._view = value;
console.log(`set this._view = ${value};`)
}
}
get iconSource(): string {
return this._iconSource;
}
set iconSource(value: string) {
if (this._iconSource !== value) {
this._iconSource = value;
console.log(`set this._iconSource = ${value};`)
}
}
}
var TAB_VIEW = "TabView";
var ITEMS = "items";
var SELECTED_INDEX = "selectedIndex";
@@ -176,4 +217,15 @@ export class TabView extends view.View implements definition.TabView, view.AddAr
}
}
}
public _onBindingContextChanged(oldValue: any, newValue: any) {
super._onBindingContextChanged(oldValue, newValue);
if (this.items && this.items.length > 0) {
var i = 0;
var length = this.items.length;
for (; i < length; i++) {
this.items[i].bindingContext = newValue;
}
}
}
}

View File

@@ -5,25 +5,26 @@ declare module "ui/tab-view" {
import view = require("ui/core/view");
import dependencyObservable = require("ui/core/dependency-observable");
import observable = require("data/observable");
import bindable = require("ui/core/bindable");
/**
* Represents a tab view entry.
*/
interface TabViewItem {
class TabViewItem extends bindable.Bindable {
/**
* Gets or sets the title of the TabViewItem.
*/
title: string;
public title: string;
/**
* Gets or sets the view of the TabViewItem.
*/
view: view.View;
public view: view.View;
/**
* Gets or sets the icon source of the TabViewItem. This could either be a a file name or resource id.
*/
iconSource?: string;
public iconSource: string;
}
/**