Merge pull request #255 from NativeScript/nnikolov/ForgottenStuff

Binding to object via  keyword.
This commit is contained in:
Nedyalko Nikolov
2015-04-03 16:31:14 +03:00
5 changed files with 73 additions and 8 deletions

View File

@ -1,3 +1,3 @@
import application = require("application");
application.mainModule = "app/mainPage";
application.mainModule = "app/testRunnerPage";
application.start();

View File

@ -461,6 +461,46 @@ export function test_usingAppLevelConvertersInListViewItems() {
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) {
if (!args.view) {
args.view = new labelModule.Label();

View File

@ -72,8 +72,9 @@ var Path = require("js-libs/polymer-expressions/path-parser").Path;
},
setValue: function (model, newValue) {
if (this.path.length == 1);
if (this.path.length == 1) {
model = findScope(model, this.path[0]);
}
return this.path.setValueFrom(model, newValue);
}

View File

@ -6,6 +6,7 @@ export module bindingConstants {
export var expression = "expression";
export var twoWay = "twoWay";
export var source = "source";
export var bindingValueKey = "$value";
};
var hasEqualSignRegex = /=+/;
@ -78,7 +79,8 @@ function parseNamedProperties(parameterList, knownOptions, callback) {
function extractPropertyNameFromExpression(expression: string): string {
var firstExpressionSymbolIndex = expression.search(expressionSymbolsRegex);
if (firstExpressionSymbolIndex > -1) {
return expression.substr(0, firstExpressionSymbolIndex).trim();
var sourceProp = expression.substr(0, firstExpressionSymbolIndex).trim();
return sourceProp;
}
else {
return expression;

View File

@ -6,6 +6,7 @@ import appModule = require("application");
import types = require("utils/types");
import trace = require("trace");
import polymerExpressions = require("js-libs/polymer-expressions");
import bindingBuilder = require("../builder/binding-builder");
var bindingContextProperty = new dependencyObservable.Property(
"bindingContext",
@ -199,7 +200,12 @@ export class Binding {
if (this.options.twoWay) {
if (this._isExpression(this.options.expression)) {
var changedModel = {};
if (this.options.sourceProperty === bindingBuilder.bindingConstants.bindingValueKey) {
changedModel[bindingBuilder.bindingConstants.bindingValueKey] = value;
}
else {
changedModel[this.options.sourceProperty] = value;
}
var expressionValue = this._getExpressionValue(this.options.expression, true, changedModel);
if (expressionValue instanceof Error) {
trace.write((<Error>expressionValue).message, trace.categories.Binding, trace.messageType.error);
@ -258,7 +264,11 @@ export class Binding {
private getSourceProperty() {
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) {
trace.write((<Error>expressionValue).message, trace.categories.Binding, trace.messageType.error);
}
@ -275,9 +285,13 @@ export class Binding {
if (this.sourceOptions) {
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);
} else if (sourceOptionsInstance && this.sourceOptions.property &&
}
else if (sourceOptionsInstance && this.sourceOptions.property &&
this.sourceOptions.property in sourceOptionsInstance) {
value = sourceOptionsInstance[this.sourceOptions.property];
}
@ -312,6 +326,14 @@ export class Binding {
private resolveOptions(obj: WeakRef<any>, property: string): { instance: any; property: any } {
var options;
if (property === bindingBuilder.bindingConstants.bindingValueKey) {
options = {
instance: obj,
property: property
};
return options;
}
if (!this._isExpression(property) && types.isString(property) && property.indexOf(".") !== -1) {
var properties = property.split(".");