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: ...}
]
我想在客户端复制这个服务器模型,所以我有一个基本的Logbook
类和多个日志(log)子类:
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) ?