I have a REST Json API that returns a list "logbooks". There are many types of logbooks that implement different but similar behavior. The server side implementation of this on the Database layer is a sort of Single Table Inheritance, so each JSON representation of a logbook contains its "type" :
[
{"type": "ULM", "name": "My uml logbook", ... , specific_uml_logbook_attr: ...},
{"type": "Plane", "name": "My plane logbook", ... , specific_plane_logbook_attr: ...}
]
I would like to replicate this server model on the client side, so I have a base Logbook
class and multiple logbook sub classes :
class Logbook extends Backbone.Model
class UmlLogbook extends Logbook
class PlaneLogbook extends Logbook
...
My Backbone.Collection
is a set of Logbook
models that i use to query the JSON API :
class LogbookCollection extends Backbone.Collection
model: Logbook
url: "/api/logbooks"
When I fetch the logbook collection, is there a way to cast each Logbook
to its corresponding sub class (based on the JSON "type" attribute) ?