Skip to content

Commit c133cda

Browse files
committed
Merge pull request NativeScript#226 from NativeScript/hdeshev/xhr-relative-paths
Support "relative" URLs in component decorators.
2 parents df63d10 + 9a35353 commit c133cda

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

.ctags-exclude

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,16 @@ bin
33
build
44
android17.d.ts
55
ios.d.ts
6-
ng-sample/app
7-
ng-sample/src/angular2
8-
ng-sample/src/nativescript-angular
6+
ng-sample/app/nativescript-angular
97
ng-sample/platforms
8+
ng-sample/node_modules
109
ng-sample/lib
1110
ng-sample/src/typings
12-
todomvc/app
13-
todomvc/src/angular2
14-
todomvc/src/nativescript-angular
15-
todomvc/platforms
16-
todomvc/lib
17-
todomvc/src/typings
18-
todomvc/typings
19-
startup-test/app
20-
startup-test/src/angular2
21-
startup-test/src/nativescript-angular
22-
startup-test/platforms
23-
startup-test/lib
24-
startup-test/src/typings
25-
src/angular2
26-
web/*.js
11+
tests/app/nativescript-angular
12+
tests/platforms
13+
tests/node_modules
14+
tests/lib
15+
tests/src/typings
16+
tests/typings
17+
web
2718
src/*.js
28-
deps/angular/dist
29-
deps/angular/tmp

src/nativescript-angular/xhr.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@ import {path, knownFolders, File} from "file-system";
22
import {XHR} from "@angular/compiler/src/xhr";
33

44
export class FileSystemXHR extends XHR {
5-
get(url: string): Promise<string> {
6-
let appDir = knownFolders.currentApp().path;
7-
let templatePath = path.join(appDir, url);
5+
resolve(url: string, baseUrl: string): string {
6+
//Angular assembles absolute URL's and prefixes them with //
7+
if (url.indexOf("//") !== 0) {
8+
//Resolve relative URL's based on the app root.
9+
return path.join(baseUrl, url);
10+
} else {
11+
return url;
12+
}
13+
}
814

9-
if (!File.exists(templatePath)) {
10-
throw new Error(`File ${url} does not exist.`);
11-
}
12-
let templateFile = File.fromPath(templatePath);
13-
return templateFile.readText();
14-
}
15+
get(url: string): Promise<string> {
16+
const appDir = knownFolders.currentApp().path;
17+
const templatePath = this.resolve(url, appDir);
18+
19+
if (!File.exists(templatePath)) {
20+
throw new Error(`File ${templatePath} does not exist. Resolved from: ${url}.`);
21+
}
22+
let templateFile = File.fromPath(templatePath);
23+
return templateFile.readText();
24+
}
1525
}

tests/app/tests/xhr-paths.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//make sure you import mocha-config before @angular/core
2+
import {assert} from "./test-config";
3+
import {FileSystemXHR} from "nativescript-angular/xhr";
4+
5+
describe("XHR name resolution", () => {
6+
it("resolves relative paths from app root", () => {
7+
const xhr = new FileSystemXHR();
8+
assert.strictEqual("/app/dir/mydir/mycomponent.html", xhr.resolve("mydir/mycomponent.html", "/app/dir"))
9+
});
10+
11+
it("resolves absolute paths as is", () => {
12+
const xhr = new FileSystemXHR();
13+
assert.strictEqual("//app/mydir/mycomponent.html", xhr.resolve("//app/mydir/mycomponent.html", "/app/dir"))
14+
});
15+
})

0 commit comments

Comments
 (0)