Skip to content

Support "relative" URLs in component decorators. #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions .ctags-exclude
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,16 @@ bin
build
android17.d.ts
ios.d.ts
ng-sample/app
ng-sample/src/angular2
ng-sample/src/nativescript-angular
ng-sample/app/nativescript-angular
ng-sample/platforms
ng-sample/node_modules
ng-sample/lib
ng-sample/src/typings
todomvc/app
todomvc/src/angular2
todomvc/src/nativescript-angular
todomvc/platforms
todomvc/lib
todomvc/src/typings
todomvc/typings
startup-test/app
startup-test/src/angular2
startup-test/src/nativescript-angular
startup-test/platforms
startup-test/lib
startup-test/src/typings
src/angular2
web/*.js
tests/app/nativescript-angular
tests/platforms
tests/node_modules
tests/lib
tests/src/typings
tests/typings
web
src/*.js
deps/angular/dist
deps/angular/tmp
28 changes: 19 additions & 9 deletions src/nativescript-angular/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@ import {path, knownFolders, File} from "file-system";
import {XHR} from "@angular/compiler/src/xhr";

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

if (!File.exists(templatePath)) {
throw new Error(`File ${url} does not exist.`);
}
let templateFile = File.fromPath(templatePath);
return templateFile.readText();
}
get(url: string): Promise<string> {
const appDir = knownFolders.currentApp().path;
const templatePath = this.resolve(url, appDir);

if (!File.exists(templatePath)) {
throw new Error(`File ${templatePath} does not exist. Resolved from: ${url}.`);
}
let templateFile = File.fromPath(templatePath);
return templateFile.readText();
}
}
15 changes: 15 additions & 0 deletions tests/app/tests/xhr-paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//make sure you import mocha-config before @angular/core
import {assert} from "./test-config";
import {FileSystemXHR} from "nativescript-angular/xhr";

describe("XHR name resolution", () => {
it("resolves relative paths from app root", () => {
const xhr = new FileSystemXHR();
assert.strictEqual("/app/dir/mydir/mycomponent.html", xhr.resolve("mydir/mycomponent.html", "/app/dir"))
});

it("resolves absolute paths as is", () => {
const xhr = new FileSystemXHR();
assert.strictEqual("//app/mydir/mycomponent.html", xhr.resolve("//app/mydir/mycomponent.html", "/app/dir"))
});
})