mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
Merge pull request #255 from NativeScript/nnikolov/ForgottenStuff
Binding to object via keyword.
This commit is contained in:
@ -1,3 +1,3 @@
|
|||||||
import application = require("application");
|
import application = require("application");
|
||||||
application.mainModule = "app/mainPage";
|
application.mainModule = "app/testRunnerPage";
|
||||||
application.start();
|
application.start();
|
||||||
|
@ -461,6 +461,46 @@ export function test_usingAppLevelConvertersInListViewItems() {
|
|||||||
helper.buildUIAndRunTest(listView, testAction);
|
helper.buildUIAndRunTest(listView, testAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function test_BindingListViewToASimpleArray() {
|
||||||
|
var listView = new listViewModule.ListView();
|
||||||
|
|
||||||
|
function testAction(views: Array<viewModule.View>) {
|
||||||
|
listView.itemTemplate = "<Label id=\"testLabel\" text=\"{{ $value }}\" />";
|
||||||
|
listView.items = [1, 2, 3];
|
||||||
|
|
||||||
|
TKUnit.wait(ASYNC);
|
||||||
|
var firstNativeElementText = getTextFromNativeElementAt(listView, 0);
|
||||||
|
var secondNativeElementText = getTextFromNativeElementAt(listView, 1);
|
||||||
|
var thirdNativeElementText = getTextFromNativeElementAt(listView, 2);
|
||||||
|
|
||||||
|
TKUnit.assertEqual(firstNativeElementText, "1", "first element text");
|
||||||
|
TKUnit.assertEqual(secondNativeElementText, "2", "second element text");
|
||||||
|
TKUnit.assertEqual(thirdNativeElementText, "3", "third element text");
|
||||||
|
}
|
||||||
|
|
||||||
|
helper.buildUIAndRunTest(listView, testAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function test_BindingListViewToASimpleArrayWithExpression() {
|
||||||
|
var listView = new listViewModule.ListView();
|
||||||
|
|
||||||
|
function testAction(views: Array<viewModule.View>) {
|
||||||
|
listView.itemTemplate = "<Label id=\"testLabel\" text=\"{{ $value, $value + ' some static text' }}\" />";
|
||||||
|
listView.items = [1, 2, 3];
|
||||||
|
|
||||||
|
TKUnit.wait(ASYNC);
|
||||||
|
var firstNativeElementText = getTextFromNativeElementAt(listView, 0);
|
||||||
|
var secondNativeElementText = getTextFromNativeElementAt(listView, 1);
|
||||||
|
var thirdNativeElementText = getTextFromNativeElementAt(listView, 2);
|
||||||
|
|
||||||
|
TKUnit.assertEqual(firstNativeElementText, "1 some static text", "first element text");
|
||||||
|
TKUnit.assertEqual(secondNativeElementText, "2 some static text", "second element text");
|
||||||
|
TKUnit.assertEqual(thirdNativeElementText, "3 some static text", "third element text");
|
||||||
|
}
|
||||||
|
|
||||||
|
helper.buildUIAndRunTest(listView, testAction);
|
||||||
|
}
|
||||||
|
|
||||||
function loadViewWithItemNumber(args: listViewModule.ItemEventData) {
|
function loadViewWithItemNumber(args: listViewModule.ItemEventData) {
|
||||||
if (!args.view) {
|
if (!args.view) {
|
||||||
args.view = new labelModule.Label();
|
args.view = new labelModule.Label();
|
||||||
|
@ -72,8 +72,9 @@ var Path = require("js-libs/polymer-expressions/path-parser").Path;
|
|||||||
},
|
},
|
||||||
|
|
||||||
setValue: function (model, newValue) {
|
setValue: function (model, newValue) {
|
||||||
if (this.path.length == 1);
|
if (this.path.length == 1) {
|
||||||
model = findScope(model, this.path[0]);
|
model = findScope(model, this.path[0]);
|
||||||
|
}
|
||||||
|
|
||||||
return this.path.setValueFrom(model, newValue);
|
return this.path.setValueFrom(model, newValue);
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ export module bindingConstants {
|
|||||||
export var expression = "expression";
|
export var expression = "expression";
|
||||||
export var twoWay = "twoWay";
|
export var twoWay = "twoWay";
|
||||||
export var source = "source";
|
export var source = "source";
|
||||||
|
export var bindingValueKey = "$value";
|
||||||
};
|
};
|
||||||
|
|
||||||
var hasEqualSignRegex = /=+/;
|
var hasEqualSignRegex = /=+/;
|
||||||
@ -78,7 +79,8 @@ function parseNamedProperties(parameterList, knownOptions, callback) {
|
|||||||
function extractPropertyNameFromExpression(expression: string): string {
|
function extractPropertyNameFromExpression(expression: string): string {
|
||||||
var firstExpressionSymbolIndex = expression.search(expressionSymbolsRegex);
|
var firstExpressionSymbolIndex = expression.search(expressionSymbolsRegex);
|
||||||
if (firstExpressionSymbolIndex > -1) {
|
if (firstExpressionSymbolIndex > -1) {
|
||||||
return expression.substr(0, firstExpressionSymbolIndex).trim();
|
var sourceProp = expression.substr(0, firstExpressionSymbolIndex).trim();
|
||||||
|
return sourceProp;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return expression;
|
return expression;
|
||||||
|
@ -6,6 +6,7 @@ import appModule = require("application");
|
|||||||
import types = require("utils/types");
|
import types = require("utils/types");
|
||||||
import trace = require("trace");
|
import trace = require("trace");
|
||||||
import polymerExpressions = require("js-libs/polymer-expressions");
|
import polymerExpressions = require("js-libs/polymer-expressions");
|
||||||
|
import bindingBuilder = require("../builder/binding-builder");
|
||||||
|
|
||||||
var bindingContextProperty = new dependencyObservable.Property(
|
var bindingContextProperty = new dependencyObservable.Property(
|
||||||
"bindingContext",
|
"bindingContext",
|
||||||
@ -199,7 +200,12 @@ export class Binding {
|
|||||||
if (this.options.twoWay) {
|
if (this.options.twoWay) {
|
||||||
if (this._isExpression(this.options.expression)) {
|
if (this._isExpression(this.options.expression)) {
|
||||||
var changedModel = {};
|
var changedModel = {};
|
||||||
|
if (this.options.sourceProperty === bindingBuilder.bindingConstants.bindingValueKey) {
|
||||||
|
changedModel[bindingBuilder.bindingConstants.bindingValueKey] = value;
|
||||||
|
}
|
||||||
|
else {
|
||||||
changedModel[this.options.sourceProperty] = value;
|
changedModel[this.options.sourceProperty] = value;
|
||||||
|
}
|
||||||
var expressionValue = this._getExpressionValue(this.options.expression, true, changedModel);
|
var expressionValue = this._getExpressionValue(this.options.expression, true, changedModel);
|
||||||
if (expressionValue instanceof Error) {
|
if (expressionValue instanceof Error) {
|
||||||
trace.write((<Error>expressionValue).message, trace.categories.Binding, trace.messageType.error);
|
trace.write((<Error>expressionValue).message, trace.categories.Binding, trace.messageType.error);
|
||||||
@ -258,7 +264,11 @@ export class Binding {
|
|||||||
|
|
||||||
private getSourceProperty() {
|
private getSourceProperty() {
|
||||||
if (this._isExpression(this.options.expression)) {
|
if (this._isExpression(this.options.expression)) {
|
||||||
var expressionValue = this._getExpressionValue(this.options.expression, false, undefined);
|
var changedModel = {};
|
||||||
|
if (this.options.sourceProperty === bindingBuilder.bindingConstants.bindingValueKey) {
|
||||||
|
changedModel[bindingBuilder.bindingConstants.bindingValueKey] = this.source.get();
|
||||||
|
}
|
||||||
|
var expressionValue = this._getExpressionValue(this.options.expression, false, changedModel);
|
||||||
if (expressionValue instanceof Error) {
|
if (expressionValue instanceof Error) {
|
||||||
trace.write((<Error>expressionValue).message, trace.categories.Binding, trace.messageType.error);
|
trace.write((<Error>expressionValue).message, trace.categories.Binding, trace.messageType.error);
|
||||||
}
|
}
|
||||||
@ -275,9 +285,13 @@ export class Binding {
|
|||||||
|
|
||||||
if (this.sourceOptions) {
|
if (this.sourceOptions) {
|
||||||
var sourceOptionsInstance = this.sourceOptions.instance.get();
|
var sourceOptionsInstance = this.sourceOptions.instance.get();
|
||||||
if (sourceOptionsInstance instanceof observable.Observable) {
|
if (this.sourceOptions.property === bindingBuilder.bindingConstants.bindingValueKey) {
|
||||||
|
value = sourceOptionsInstance;
|
||||||
|
}
|
||||||
|
else if (sourceOptionsInstance instanceof observable.Observable) {
|
||||||
value = sourceOptionsInstance.get(this.sourceOptions.property);
|
value = sourceOptionsInstance.get(this.sourceOptions.property);
|
||||||
} else if (sourceOptionsInstance && this.sourceOptions.property &&
|
}
|
||||||
|
else if (sourceOptionsInstance && this.sourceOptions.property &&
|
||||||
this.sourceOptions.property in sourceOptionsInstance) {
|
this.sourceOptions.property in sourceOptionsInstance) {
|
||||||
value = sourceOptionsInstance[this.sourceOptions.property];
|
value = sourceOptionsInstance[this.sourceOptions.property];
|
||||||
}
|
}
|
||||||
@ -312,6 +326,14 @@ export class Binding {
|
|||||||
private resolveOptions(obj: WeakRef<any>, property: string): { instance: any; property: any } {
|
private resolveOptions(obj: WeakRef<any>, property: string): { instance: any; property: any } {
|
||||||
var options;
|
var options;
|
||||||
|
|
||||||
|
if (property === bindingBuilder.bindingConstants.bindingValueKey) {
|
||||||
|
options = {
|
||||||
|
instance: obj,
|
||||||
|
property: property
|
||||||
|
};
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
if (!this._isExpression(property) && types.isString(property) && property.indexOf(".") !== -1) {
|
if (!this._isExpression(property) && types.isString(property) && property.indexOf(".") !== -1) {
|
||||||
var properties = property.split(".");
|
var properties = property.split(".");
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user