So with the idea that angular2 will support multiple rendering engines (HTML, native via NativeScript & React Native) what does that development story look like?
Is there dynamic template switching? Or should this be handled via sub-classing?
// TypeScript ahead
// Base component implementation
class FooComponent {
public name: string = 'my name';
public makeFormal () {
this.name = this.name.toUpperCase();
}
}
// HTML Component
@Component({
selector: '<foo></foo>'
template: `
<div>{{name}}</div>
<button (click)="makeFormal()">Make Formal</button>
`
})
class FooHtmlComponent extends FooComponent {
constructor(){
super();
}
}
// Native Component
@Component({
selector: '<foo></foo>'
template: '... [native stuffs here] ...'
})
class FooHtmlComponent extends FooComponent {
constructor(){
super();
}
}
Subclassing a component is one way to do that.
You can use multiple view decorators cause the following:
@Component({selector:'foo', template: `some nativescript template`})
class Foo {}
is the same as:
@Component({selector:'foo'`})
@View({
template: `some nativescript template`
})
class Foo {}
Next, you will be able to provide multiple views for the same component.
@Component({selector:'foo'})
@View({
template: `some nativescript template`,
platform: 'nativescript'
})
@View({
template: `some dom stuff`,
platform: 'dom'
})
class Foo {
}
Finally, a build step will create a bundle for every platform with all the code targeting other platforms removed. The same technique can be used for providing language-specific templates for components.
So you can just do the following:
@Component({selector:'foo', template: `some dom template`})
class Foo {}
NOTE: This could all change because Angular 2 is still alpha.
There were several good talks at AngularConnect 2015 about this.
In particular, though, you're going to want to checkout Angular Universal which is the "Universal" (aka Isomorphic) (aka app primed on the server) rendering method for Angular 2.
Also for rendering to React-Native, there is a library growing here that has more information about that.
I hope that helps.
(PS: marked community wiki because the answer is so vague) ;)
When the first glimpses of Angular 2 began to surface more than a year ago, this is the one particular area that was the most interesting to me. Originally, I was thinking more along the lines of what Victor mentioned above regarding having multiple @View decorators for each target platform. This is either still in the works, undocumented, or no longer planned (I'm not sure?).
However, as things evolved, I began to see a cleaner integration potential via the use of custom Decorators
. I have detailed this strategy here: http://angularjs.blogspot.com/2016/03/code-reuse-in-angular-2-native-mobile.html
Additionally, I have proven out this strategy here: https://github.com/NathanWalker/angular2-seed-advanced
I'm looking forward to other alternative approaches and still not sure what is best (if there is such thing), but I like the custom Decorator approach so far.
The thought of having build steps which would build out different versions of your application with the correct @View
decorator as mentioned by Victor still sounds very helpful and hope that is (in some form at least) still on the roadmap for Angular 2.0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With