A remote, third-party JSONP server provides my CanJS script with a list of results like this one:
[
{ "class": "ABaseClass", "value": "1"},
{ "class": "ASubClass", "value": "2"},
{ "class": "ABaseClass", "value": "3"},
{ "class": "ASubClass", "value": "4"},
...
]
where type
is the intended object class, defined in CanJS using can.Model.extend
:
The following simplified code demonstrates the CanJS setup:
ABaseClass = can.Model.extend({ ... }, {
'findAll': { 'url': 'the url', 'dataType': "jsonp" }
// this is modified source code and may not run
});
// ASubClass is a subclass of ABaseClass.
ASubClass = ABaseClass.extend({ ... }, { ... });
Problem:
When ABaseClass.findAll({}, function(data) { ... })
is called, which calls the JSONP endpoints for more objects, the callback obtains a list of CanJS models, but only of class ABaseClass
.
Question:
Is there a helper method provided by CanJS to automatically create subclasses based on a field within a list of objects? If not, how can I go about implementing one?
Expected output:
[
(new ABaseClass(...)),
(new ASubClass(...)),
(new ABaseClass(...)),
(new ASubClass(...)),
...
]
Environment:
- CanJS: 1.17
- jQuery: 1.10.1
- I cannot control what types of objects the endpoint returns.
- Multiple AJAX calls is not an accepted solution.