Interesting point. I never thought that the generic type you could pass to .get(), .post(), etc, could be used by Angular to perform the conversion. Do you have examples of that being used?
The case for adapters is also handling complex conversions, such as building nested objects and the like. For example, what if GET: /courses/ returned course objects with a students field, representing a list of Student objects? Can generic type conversion handle that?
Also, how can generic type conversion decide to build a Date object out of the created field (a string)?
That code snippet works like a charm i’m using it currently it been like that since angular 5 or 4 not sure, your right about the date thingy but if you send it from backend as a date type. You’ll get it as a date type too in angular.
the generic type you are able to pass to an http get or post, etc... its merely to allow typed responses. The adapter is still needed if your model objects contain any methods, etc. So for instance if you have a method such a getFullName() within your User model, this wont be available for the objects returned by http.get. its still necessary to do a map in order to actually create the User objects based on the data received. Hope this clarifies a bit
Interesting point. I never thought that the generic type you could pass to
.get()
,.post()
, etc, could be used by Angular to perform the conversion. Do you have examples of that being used?The case for adapters is also handling complex conversions, such as building nested objects and the like. For example, what if
GET: /courses/
returned course objects with astudents
field, representing a list ofStudent
objects? Can generic type conversion handle that?Also, how can generic type conversion decide to build a
Date
object out of thecreated
field (a string)?Happy to discuss further. :)
That code snippet works like a charm i’m using it currently it been like that since angular 5 or 4 not sure, your right about the date thingy but if you send it from backend as a date type. You’ll get it as a date type too in angular.
Cool! Glad to hear simple type conversions work.
To my knowledge there is no
Date
type in JSON, though, so that still means the model-adapter pattern is needed in more complex use cases.In typescript/JavaScript there is obviously, you’ll access it like a normal property Obj.dateProperty
the generic type you are able to pass to an http get or post, etc... its merely to allow typed responses. The adapter is still needed if your model objects contain any methods, etc. So for instance if you have a method such a getFullName() within your User model, this wont be available for the objects returned by http.get. its still necessary to do a map in order to actually create the User objects based on the data received. Hope this clarifies a bit
angular.io/guide/http#requesting-a...