Skip to content

Commit 659182c

Browse files
committed
Merge pull request #124 from NativeScript/feature/nested-properties
Allow settingnested properties
2 parents c2224b9 + 074d96a commit 659182c

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

ng-sample/.vscode/settings.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"when": "$(basename).ts"
66
},
77
"**/*.js.map": {},
8-
"platforms/**": {},
9-
"hooks/**": {}
8+
"platforms/**": {}
109
}
1110
}

ng-sample/app/examples/action-bar/action-bar-test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {NS_DIRECTIVES} from "../../nativescript-angular/directives/ns-directives
1010
template: `
1111
<ActionBar title="Custom Title">
1212
<ActionItem *ngIf="show" text="action" (tap)="onTap()"></ActionItem>
13+
<ActionItem ios.systemIcon="9" android.systemIcon="ic_menu_share_holo_light" (tap)="onShare()"></ActionItem>
1314
</ActionBar>
1415
1516
<StackLayout verticalAlignment="center">
@@ -23,6 +24,9 @@ class FirstComponent {
2324
onTap() {
2425
console.log("FirstComponent.Tapped!");
2526
}
27+
onShare() {
28+
console.log("Share button tapped!");
29+
}
2630
}
2731

2832

src/nativescript-angular/view-util.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isString} from "utils/types";
1+
import {isString, isDefined} from "utils/types";
22
import {View} from "ui/core/view";
33
import {Placeholder} from "ui/placeholder";
44
import {ContentView} from 'ui/content-view';
@@ -139,6 +139,31 @@ function isXMLAttribute(name: string): boolean {
139139
}
140140

141141
export function setProperty(view: NgView, attributeName: string, value: any): void {
142+
if (attributeName.indexOf(".") !== -1) {
143+
// Handle nested properties
144+
const properties = attributeName.split(".");
145+
attributeName = properties[properties.length - 1];
146+
147+
let propMap = getProperties(view);
148+
let i = 0;
149+
while (i < properties.length - 1 && isDefined(view)) {
150+
var prop = properties[i];
151+
if (propMap.has(prop)) {
152+
prop = propMap.get(prop);
153+
}
154+
155+
view = view[prop];
156+
propMap = getProperties(view);
157+
i++;
158+
}
159+
}
160+
161+
if (isDefined(view)) {
162+
setPropertyInternal(view, attributeName, value);
163+
}
164+
}
165+
166+
function setPropertyInternal(view: NgView, attributeName: string, value: any): void {
142167
traceLog('Setting attribute: ' + attributeName);
143168

144169
let specialSetter = getSpecialPropertySetter(attributeName);
@@ -161,7 +186,7 @@ export function setProperty(view: NgView, attributeName: string, value: any): vo
161186
}
162187

163188
function convertValue(value: any): any {
164-
if (typeof(value) !== "string" || value === "") {
189+
if (typeof (value) !== "string" || value === "") {
165190
return value;
166191
}
167192

tests/app/tests/property-sets.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class TestView extends View implements ViewExtensions {
1717
public numValue: number = 0;
1818
public boolValue: boolean = undefined;
1919
public anyValue: any = undefined;
20+
public nested: { property: string } = { property: "untouched" };
2021
}
2122

2223
describe('setting View properties', () => {
@@ -63,9 +64,16 @@ describe('setting View properties', () => {
6364
});
6465

6566
it('preserves objects', () => {
66-
let value = {name: "Jim", age: 23};
67+
let value = { name: "Jim", age: 23 };
6768
let view = new TestView();
6869
setProperty(view, "anyValue", value)
6970
assert.deepEqual(value, view.anyValue);
7071
});
72+
73+
it('sets nested properties', () => {
74+
let view = new TestView();
75+
setProperty(view, "nested.property", "blah")
76+
assert.strictEqual("blah", view.nested.property);
77+
});
78+
7179
});

0 commit comments

Comments
 (0)