After toying with Angular 2 using strictly static content, I ventured into HTTP/Observable land to see what came of it. Observables are a nice alternative to Promises - although the benefits when compared to that alternative are lost on me for now. But there is one problem that's been puzzling me this afternoon.
When fetching data using Observables (as per https://angular.io/docs/ts/latest/guide/server-communication.html), it seems the class corresponding to the model is not loaded. This is not a problem as long as only properties are used, but it prevents using methods that can be defined inside that class.
Plunker : http://plnkr.co/edit/WjEfdOHmHWT2RZVDSkjr?p=preview
You can materialize the problem by alternating between the two instructions defined in ngOnInit()
in the App component:
ngOnInit(){
// NB
// Alternate between the two instructions below while checking
// the 'Network' tab of your devtool of choice.
// Doing this will actually load the 'person.model.ts' file,
// and make the methods available to the component and its template
this.people[0] = new Person();
// Doing this will NOT load the 'person.model.ts' file, thus
// causing any call to a method defined in that class to fail.
//this.getPeople();
}
When the first instruction is used, the method person.greet()
works just fine when called via interpolation in the template, and the corresponding file is fetched. But when using the second instruction, an error is raised and logged to the console. By the way this error appears on the Plunker I made - I get nothing when met with the problem on my own application.
Now it is probably possible to bypass the problem (for example, making a dummy call to new Person()
before the subscription kicks in), but this does feel really clumsy. Something similar is suggested here : https://stackoverflow.com/a/34777339/5751783 (although I'm not completely sure that SO question is related).
Is this a real problem, or am I doing something wrong ? And if there is indeed an issue at hand, what would be the proper way of preventing it ?
Thanks in advance !
Disclaimer : if there is an actual issue, I'm not sure if it's related to Angular2, SystemJS, RxJS or some other component used under the hood. I'll use the tag angular2 for the time being (given that's the reason I'm here), but I'll add more (or remove angular2) depending on the answers.