mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Binding to ViewBase as source now works (#4195)
* Fix 4127 * Move back Binding class to bindable module
This commit is contained in:
@@ -24,9 +24,7 @@ page.id = "mainPage";
|
||||
page.on(Page.navigatedToEvent, onNavigatedTo);
|
||||
|
||||
function runTests() {
|
||||
setTimeout(function () {
|
||||
tests.runAll();
|
||||
}, 10);
|
||||
setTimeout(() => tests.runAll(), 10);
|
||||
}
|
||||
|
||||
function onNavigatedTo(args) {
|
||||
|
||||
@@ -29,6 +29,15 @@ export function test_Bindable_Members() {
|
||||
TKUnit.assert(types.isDefined(obj.unbind), "Bindable.unbind not defined");
|
||||
};
|
||||
|
||||
export function test_Binding_to_bindingContext_of_View() {
|
||||
const target = new Button();
|
||||
const source = new Button();
|
||||
|
||||
target.bind({ targetProperty: "bindingContext", sourceProperty: "text" }, source);
|
||||
source.text = 'a';
|
||||
TKUnit.assertEqual(target.bindingContext, 'a');
|
||||
};
|
||||
|
||||
export function test_Bindable_Bind_ToTarget_OneWay() {
|
||||
const model = new Observable();
|
||||
model.set("name", "John");
|
||||
|
||||
@@ -150,6 +150,13 @@ export class Observable {
|
||||
*/
|
||||
_createPropertyChangeData(name: string, value: any, oldValue?: any): PropertyChangeData;
|
||||
|
||||
//@private
|
||||
/**
|
||||
* Filed to use instead of instanceof ViewBase.
|
||||
* @private
|
||||
*/
|
||||
public _isViewBase: boolean;
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
|
||||
@@ -32,6 +32,8 @@ let _wrappedValues = [
|
||||
|
||||
export class Observable implements ObservableDefinition {
|
||||
public static propertyChangeEvent = "propertyChange";
|
||||
public _isViewBase: boolean;
|
||||
|
||||
private _observers = {};
|
||||
|
||||
public get(name: string): any {
|
||||
@@ -233,4 +235,4 @@ export function fromObjectRecursive(source: any): Observable {
|
||||
let observable = new ObservableFromObject();
|
||||
addPropertiesFromObject(observable, source, true);
|
||||
return observable;
|
||||
}
|
||||
}
|
||||
@@ -59,4 +59,4 @@ export class Binding {
|
||||
}
|
||||
|
||||
export function getEventOrGestureName(name: string): string;
|
||||
export function isEventOrGesture(name: string, view: ViewBase): boolean;
|
||||
export function isEventOrGesture(name: string, view: ViewBase): boolean;
|
||||
@@ -22,10 +22,9 @@ const contextKey = "context";
|
||||
// from $parents['ListView'] will return 'ListView'
|
||||
// from $parents[1] will return 1
|
||||
const paramsRegex = /\[\s*(['"])*(\w*)\1\s*\]/;
|
||||
|
||||
const bc = bindingConstants;
|
||||
|
||||
const emptyArray = [];
|
||||
|
||||
function getProperties(property: string): Array<string> {
|
||||
let result: Array<string> = emptyArray;
|
||||
if (property) {
|
||||
@@ -55,7 +54,7 @@ export function getEventOrGestureName(name: string): string {
|
||||
// NOTE: method fromString from "ui/gestures";
|
||||
export function isGesture(eventOrGestureName: string): boolean {
|
||||
let t = eventOrGestureName.trim().toLowerCase();
|
||||
return t === "tap"
|
||||
return t === "tap"
|
||||
|| t === "doubletap"
|
||||
|| t === "pinch"
|
||||
|| t === "pan"
|
||||
@@ -169,8 +168,9 @@ export class Binding {
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.value) {
|
||||
this.update(data.value);
|
||||
const value = data.value;
|
||||
if (value !== null && value !== undefined) {
|
||||
this.update(value);
|
||||
} else {
|
||||
// TODO: Is this correct?
|
||||
// What should happen when bindingContext is null/undefined?
|
||||
@@ -279,14 +279,16 @@ export class Binding {
|
||||
let prop = parentProperies || "";
|
||||
|
||||
for (let i = 0, length = objectsAndProperties.length; i < length; i++) {
|
||||
prop += "$" + objectsAndProperties[i].property;
|
||||
const propName = objectsAndProperties[i].property;
|
||||
prop += "$" + propName;
|
||||
let currentObject = objectsAndProperties[i].instance;
|
||||
if (!this.propertyChangeListeners.has(prop) && currentObject instanceof Observable) {
|
||||
addWeakEventListener(
|
||||
currentObject,
|
||||
Observable.propertyChangeEvent,
|
||||
this.onSourcePropertyChanged,
|
||||
this);
|
||||
if (!this.propertyChangeListeners.has(prop) && currentObject instanceof Observable && currentObject._isViewBase) {
|
||||
// Add listener for properties created with after 3.0 version
|
||||
addWeakEventListener(currentObject, `${propName}Change`, this.onSourcePropertyChanged, this);
|
||||
addWeakEventListener(currentObject, Observable.propertyChangeEvent, this.onSourcePropertyChanged, this);
|
||||
this.propertyChangeListeners.set(prop, currentObject);
|
||||
} else if (!this.propertyChangeListeners.has(prop) && currentObject instanceof Observable) {
|
||||
addWeakEventListener(currentObject, Observable.propertyChangeEvent, this.onSourcePropertyChanged, this);
|
||||
this.propertyChangeListeners.set(prop, currentObject);
|
||||
}
|
||||
}
|
||||
@@ -601,4 +603,4 @@ export class Binding {
|
||||
|
||||
this.updating = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import { Order, FlexGrow, FlexShrink, FlexWrapBefore, AlignSelf } from "../../la
|
||||
import { Length } from "../../styling/style-properties";
|
||||
|
||||
export { isIOS, isAndroid, layout, Color };
|
||||
|
||||
export * from "../properties";
|
||||
export * from "../bindable";
|
||||
|
||||
@@ -291,6 +292,12 @@ export abstract class ViewBase extends Observable {
|
||||
//@endprivate
|
||||
}
|
||||
|
||||
export class Binding {
|
||||
constructor(target: ViewBase, options: BindingOptions);
|
||||
public bind(source: Object): void;
|
||||
public unbind();
|
||||
}
|
||||
|
||||
export const idProperty: Property<ViewBase, string>;
|
||||
export const classNameProperty: Property<ViewBase, string>;
|
||||
export const bindingContextProperty: InheritedProperty<ViewBase, any>;
|
||||
@@ -299,4 +306,4 @@ export const bindingContextProperty: InheritedProperty<ViewBase, any>;
|
||||
* Converts string into boolean value.
|
||||
* Throws error if value is not 'true' or 'false'.
|
||||
*/
|
||||
export function booleanConverter(v: string): boolean;
|
||||
export function booleanConverter(v: string): boolean;
|
||||
@@ -863,6 +863,7 @@ ViewBase.prototype._defaultPaddingTop = 0;
|
||||
ViewBase.prototype._defaultPaddingRight = 0;
|
||||
ViewBase.prototype._defaultPaddingBottom = 0;
|
||||
ViewBase.prototype._defaultPaddingLeft = 0;
|
||||
ViewBase.prototype._isViewBase = true;
|
||||
|
||||
ViewBase.prototype._batchUpdateScope = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user