Skip to content

Commit daa2b36

Browse files
author
Nedyalko Nikolov
committed
Merge pull request #46 from NativeScript/nnikolov/SimpleBootstrap
Added a wrapper function that start {N} with angular2.
2 parents 26903e0 + d6db55c commit daa2b36

File tree

6 files changed

+92
-44
lines changed

6 files changed

+92
-44
lines changed

ng-sample/app/app.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import "globals";
1+
//import "globals";
22
// import "./modules";
3-
global.registerModule("./main-page", function () { return require("./main-page"); });
3+
//global.registerModule("./main-page", function () { return require("./main-page"); });
44

55
//import * as profiling from "./profiling";
66
//profiling.start('application-start');
7-
import application = require("application");
87

9-
//TODO: hide this in a separate module e.g. "angular-application"
10-
application.mainModule = "./main-page";
11-
application.cssFile = "./app.css";
12-
application.start();
8+
// this import should be first in order to load some required settings (like globals and reflect-metadata)
9+
import { nativeScriptBootstrap } from "./nativescript-angular/application";
10+
11+
//import {RendererTest} from './examples/renderer-test';
12+
//import {Benchmark} from './performance/benchmark';
13+
//import {ListTest} from './examples/list/list-test';
14+
import {ListTestAsync} from "./examples/list/list-test-async";
15+
16+
//nativeScriptBootstrap(RendererTest);
17+
//nativeScriptBootstrap(Benchmark);
18+
//nativeScriptBootstrap(ListTest);
19+
nativeScriptBootstrap(ListTestAsync);

ng-sample/app/main-page.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {topmost} from 'ui/frame';
33
import {TextView} from 'ui/text-view';
44
import {Page} from 'ui/page';
55

6-
import {nativeScriptBootstrap} from './nativescript-angular/application';
6+
import {bootstrap} from './nativescript-angular/application';
77
// import {Benchmark} from './performance/benchmark';
88
// import {RendererTest} from './examples/renderer-test';
99
// import {ListTest} from './examples/list/list-test';
@@ -19,11 +19,11 @@ export function createPage() {
1919

2020
profiling.start('ng-bootstrap');
2121
console.log('BOOTSTRAPPING...');
22-
// nativeScriptBootstrap(Benchmark, []).then((appRef) => {
23-
// nativeScriptBootstrap(RendererTest, []).then((appRef) => {
24-
// nativeScriptBootstrap(ListTest, []).then((appRef) => {
25-
// nativeScriptBootstrap(ListTestAsync, []).then((appRef) => {
26-
nativeScriptBootstrap(ImageTest, []).then((appRef) => {
22+
//bootstrap(Benchmark, []).then((appRef) => {
23+
// bootstrap(RendererTest, []).then((appRef) => {
24+
//bootstrap(ListTest, []).then((appRef) => {
25+
//bootstrap(ListTestAsync, []).then((appRef) => {
26+
bootstrap(ImageTest, []).then((appRef) => {
2727
profiling.stop('ng-bootstrap');
2828
console.log('ANGULAR BOOTSTRAP DONE.');
2929
}, (err) =>{

ng-sample/app/performance/benchmark.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class TreeComponent {
3232
template: `
3333
<StackLayout>
3434
<Label text='Benchmark!' fontSize='20' verticalAlignment='center' padding='20'></Label>
35-
<Button Text="Baseline test" (tap)="baselineTest(baseline)"></Button>
35+
<Button text="Baseline test" (tap)="baselineTest(baseline)"></Button>
3636
<StackLayout #baseline></StackLayout>
37-
<Button Text="Component test" (tap)="componentTest()"></Button>
37+
<Button text="Component test" (tap)="componentTest()"></Button>
3838
<StackLayout #component>
3939
<tree [data]='initDataNg'></tree>
4040
</StackLayout>

ng-sample/gruntfile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ module.exports = function(grunt) {
6666
"updateModules",
6767
"updateAngular",
6868
]);
69+
70+
grunt.registerTask("default", ["prepare"]);
6971
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Type, ApplicationRef, Provider } from 'angular2/core';
22

33
export type BindingArray = Array<Type | Provider | Array<any>>;
4-
export function nativeScriptBootstrap(appComponentType: any, componentInjectableBindings?: BindingArray): Promise<ApplicationRef>;
4+
export function bootstrap(appComponentType: any, componentInjectableBindings?: BindingArray, appOptions?: any): Promise<ApplicationRef>;
5+
export function nativeScriptBootstrap(appComponentType: any, customProviders?: BindingArray, appOptions?: any): void;

src/nativescript-angular/application.ts

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,72 @@ import {NS_DIRECTIVES} from './directives/ns-directives';
2828

2929
import {bootstrap as angularBootstrap} from 'angular2/bootstrap';
3030

31+
import { Page } from 'ui/page';
32+
import {topmost} from 'ui/frame';
33+
import {TextView} from 'ui/text-view';
34+
import application = require('application');
35+
3136
export type ProviderArray = Array<Type | Provider | any[]>;
3237

33-
export function nativeScriptBootstrap(appComponentType: any,
34-
customProviders: ProviderArray = null): Promise<ComponentRef> {
35-
NativeScriptDomAdapter.makeCurrent();
36-
37-
let nativeScriptProviders: ProviderArray = [
38-
NativeScriptRootRenderer,
39-
provide(RootRenderer, {useClass: NativeScriptRootRenderer}),
40-
NativeScriptRenderer,
41-
provide(Renderer, {useClass: NativeScriptRenderer}),
42-
provide(XHR, {useClass: FileSystemXHR}),
43-
provide(ExceptionHandler, {useFactory: () => new ExceptionHandler(DOM, true), deps: []}),
44-
45-
provide(PLATFORM_PIPES, {useValue: COMMON_PIPES, multi: true}),
46-
provide(PLATFORM_DIRECTIVES, {useValue: COMMON_DIRECTIVES, multi: true}),
47-
provide(PLATFORM_DIRECTIVES, {useValue: NS_DIRECTIVES, multi: true}),
48-
49-
APPLICATION_COMMON_PROVIDERS,
50-
COMPILER_PROVIDERS,
51-
PLATFORM_COMMON_PROVIDERS,
52-
FORM_PROVIDERS,
53-
];
54-
55-
var appProviders = [];
56-
if (isPresent(customProviders)) {
57-
appProviders.push(customProviders);
58-
}
59-
60-
return platform(nativeScriptProviders).application(appProviders).bootstrap(appComponentType);
38+
export function bootstrap(appComponentType: any,
39+
customProviders: ProviderArray = null, appOptions: any = null) : Promise<ComponentRef> {
40+
NativeScriptDomAdapter.makeCurrent();
41+
42+
let nativeScriptProviders: ProviderArray = [
43+
NativeScriptRootRenderer,
44+
provide(RootRenderer, {useClass: NativeScriptRootRenderer}),
45+
NativeScriptRenderer,
46+
provide(Renderer, {useClass: NativeScriptRenderer}),
47+
provide(XHR, {useClass: FileSystemXHR}),
48+
provide(ExceptionHandler, {useFactory: () => new ExceptionHandler(DOM, true), deps: []}),
49+
50+
provide(PLATFORM_PIPES, {useValue: COMMON_PIPES, multi: true}),
51+
provide(PLATFORM_DIRECTIVES, {useValue: COMMON_DIRECTIVES, multi: true}),
52+
provide(PLATFORM_DIRECTIVES, {useValue: NS_DIRECTIVES, multi: true}),
53+
54+
APPLICATION_COMMON_PROVIDERS,
55+
COMPILER_PROVIDERS,
56+
PLATFORM_COMMON_PROVIDERS,
57+
FORM_PROVIDERS,
58+
];
59+
60+
var appProviders = [];
61+
if (isPresent(customProviders)) {
62+
appProviders.push(customProviders);
63+
}
64+
65+
return platform(nativeScriptProviders).application(appProviders).bootstrap(appComponentType);
66+
}
67+
68+
export function nativeScriptBootstrap(appComponentType: any, customProviders?: ProviderArray, appOptions?: any) {
69+
if (appOptions && appOptions.cssFile) {
70+
application.cssFile = appOptions.cssFile;
71+
}
72+
application.start({
73+
create: (): Page => {
74+
let page = new Page();
75+
76+
page.on('loaded', (args) => {
77+
//profiling.stop('application-start');
78+
console.log('Page loaded');
79+
80+
//profiling.start('ng-bootstrap');
81+
console.log('BOOTSTRAPPING...');
82+
bootstrap(appComponentType).then((appRef) => {
83+
//profiling.stop('ng-bootstrap');
84+
console.log('ANGULAR BOOTSTRAP DONE.');
85+
}, (err) =>{
86+
console.log('ERROR BOOTSTRAPPING ANGULAR');
87+
let errorMessage = err.message + "\n\n" + err.stack;
88+
console.log(errorMessage);
89+
90+
let view = new TextView();
91+
view.text = errorMessage;
92+
topmost().currentPage.content = view;
93+
});
94+
});
95+
96+
return page;
97+
}
98+
});
6199
}

0 commit comments

Comments
 (0)