Skip to content

Commit 188a1c2

Browse files
author
Hristo Hristov
authored
Binding to ViewBase as source now works (#4195)
* Fix 4127 * Move back Binding class to bindable module
1 parent 34aec12 commit 188a1c2

File tree

8 files changed

+45
-19
lines changed

8 files changed

+45
-19
lines changed

tests/app/app/mainPage.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ page.id = "mainPage";
2424
page.on(Page.navigatedToEvent, onNavigatedTo);
2525

2626
function runTests() {
27-
setTimeout(function () {
28-
tests.runAll();
29-
}, 10);
27+
setTimeout(() => tests.runAll(), 10);
3028
}
3129

3230
function onNavigatedTo(args) {

tests/app/ui/core/bindable/bindable-tests.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ export function test_Bindable_Members() {
2929
TKUnit.assert(types.isDefined(obj.unbind), "Bindable.unbind not defined");
3030
};
3131

32+
export function test_Binding_to_bindingContext_of_View() {
33+
const target = new Button();
34+
const source = new Button();
35+
36+
target.bind({ targetProperty: "bindingContext", sourceProperty: "text" }, source);
37+
source.text = 'a';
38+
TKUnit.assertEqual(target.bindingContext, 'a');
39+
};
40+
3241
export function test_Bindable_Bind_ToTarget_OneWay() {
3342
const model = new Observable();
3443
model.set("name", "John");

tns-core-modules/data/observable/observable.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ export class Observable {
150150
*/
151151
_createPropertyChangeData(name: string, value: any, oldValue?: any): PropertyChangeData;
152152

153+
//@private
154+
/**
155+
* Filed to use instead of instanceof ViewBase.
156+
* @private
157+
*/
158+
public _isViewBase: boolean;
159+
153160
/**
154161
* @private
155162
*/

tns-core-modules/data/observable/observable.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ let _wrappedValues = [
3232

3333
export class Observable implements ObservableDefinition {
3434
public static propertyChangeEvent = "propertyChange";
35+
public _isViewBase: boolean;
36+
3537
private _observers = {};
3638

3739
public get(name: string): any {
@@ -233,4 +235,4 @@ export function fromObjectRecursive(source: any): Observable {
233235
let observable = new ObservableFromObject();
234236
addPropertiesFromObject(observable, source, true);
235237
return observable;
236-
}
238+
}

tns-core-modules/ui/core/bindable/bindable.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ export class Binding {
5959
}
6060

6161
export function getEventOrGestureName(name: string): string;
62-
export function isEventOrGesture(name: string, view: ViewBase): boolean;
62+
export function isEventOrGesture(name: string, view: ViewBase): boolean;

tns-core-modules/ui/core/bindable/bindable.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ const contextKey = "context";
2222
// from $parents['ListView'] will return 'ListView'
2323
// from $parents[1] will return 1
2424
const paramsRegex = /\[\s*(['"])*(\w*)\1\s*\]/;
25-
2625
const bc = bindingConstants;
27-
2826
const emptyArray = [];
27+
2928
function getProperties(property: string): Array<string> {
3029
let result: Array<string> = emptyArray;
3130
if (property) {
@@ -55,7 +54,7 @@ export function getEventOrGestureName(name: string): string {
5554
// NOTE: method fromString from "ui/gestures";
5655
export function isGesture(eventOrGestureName: string): boolean {
5756
let t = eventOrGestureName.trim().toLowerCase();
58-
return t === "tap"
57+
return t === "tap"
5958
|| t === "doubletap"
6059
|| t === "pinch"
6160
|| t === "pan"
@@ -169,8 +168,9 @@ export class Binding {
169168
return;
170169
}
171170

172-
if (data.value) {
173-
this.update(data.value);
171+
const value = data.value;
172+
if (value !== null && value !== undefined) {
173+
this.update(value);
174174
} else {
175175
// TODO: Is this correct?
176176
// What should happen when bindingContext is null/undefined?
@@ -279,14 +279,16 @@ export class Binding {
279279
let prop = parentProperies || "";
280280

281281
for (let i = 0, length = objectsAndProperties.length; i < length; i++) {
282-
prop += "$" + objectsAndProperties[i].property;
282+
const propName = objectsAndProperties[i].property;
283+
prop += "$" + propName;
283284
let currentObject = objectsAndProperties[i].instance;
284-
if (!this.propertyChangeListeners.has(prop) && currentObject instanceof Observable) {
285-
addWeakEventListener(
286-
currentObject,
287-
Observable.propertyChangeEvent,
288-
this.onSourcePropertyChanged,
289-
this);
285+
if (!this.propertyChangeListeners.has(prop) && currentObject instanceof Observable && currentObject._isViewBase) {
286+
// Add listener for properties created with after 3.0 version
287+
addWeakEventListener(currentObject, `${propName}Change`, this.onSourcePropertyChanged, this);
288+
addWeakEventListener(currentObject, Observable.propertyChangeEvent, this.onSourcePropertyChanged, this);
289+
this.propertyChangeListeners.set(prop, currentObject);
290+
} else if (!this.propertyChangeListeners.has(prop) && currentObject instanceof Observable) {
291+
addWeakEventListener(currentObject, Observable.propertyChangeEvent, this.onSourcePropertyChanged, this);
290292
this.propertyChangeListeners.set(prop, currentObject);
291293
}
292294
}
@@ -601,4 +603,4 @@ export class Binding {
601603

602604
this.updating = false;
603605
}
604-
}
606+
}

tns-core-modules/ui/core/view-base/view-base.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Order, FlexGrow, FlexShrink, FlexWrapBefore, AlignSelf } from "../../la
1717
import { Length } from "../../styling/style-properties";
1818

1919
export { isIOS, isAndroid, layout, Color };
20+
2021
export * from "../properties";
2122
export * from "../bindable";
2223

@@ -291,6 +292,12 @@ export abstract class ViewBase extends Observable {
291292
//@endprivate
292293
}
293294

295+
export class Binding {
296+
constructor(target: ViewBase, options: BindingOptions);
297+
public bind(source: Object): void;
298+
public unbind();
299+
}
300+
294301
export const idProperty: Property<ViewBase, string>;
295302
export const classNameProperty: Property<ViewBase, string>;
296303
export const bindingContextProperty: InheritedProperty<ViewBase, any>;
@@ -299,4 +306,4 @@ export const bindingContextProperty: InheritedProperty<ViewBase, any>;
299306
* Converts string into boolean value.
300307
* Throws error if value is not 'true' or 'false'.
301308
*/
302-
export function booleanConverter(v: string): boolean;
309+
export function booleanConverter(v: string): boolean;

tns-core-modules/ui/core/view-base/view-base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ ViewBase.prototype._defaultPaddingTop = 0;
863863
ViewBase.prototype._defaultPaddingRight = 0;
864864
ViewBase.prototype._defaultPaddingBottom = 0;
865865
ViewBase.prototype._defaultPaddingLeft = 0;
866+
ViewBase.prototype._isViewBase = true;
866867

867868
ViewBase.prototype._batchUpdateScope = 0;
868869

0 commit comments

Comments
 (0)