My CanJS script receives a list of results from a remote JSONP server in this format:
[
{ "class": "ABaseClass", "value": "1"},
{ "class": "ASubClass", "value": "2"},
{ "class": "ABaseClass", "value": "3"},
{ "class": "ASubClass", "value": "4"},
...
]
Type
refers to the object's class, defined in CanJS using can.Model.extend
:
The example code below shows how CanJS is set up:
ABaseClass = can.Model.extend({ ... }, {
'findAll': { 'url': 'the url', 'dataType': "jsonp" }
});
// ASubClass is a subclass of ABaseClass.
ASubClass = ABaseClass.extend({ ... }, { ... });
Issue:
When
ABaseClass.findAll({}, function(data) { ... })
makes a call to the JSONP endpoint for more objects, the callback only retrieves models of class ABaseClass
.
Query:
Does CanJS offer a method to automatically create subclasses based on a field within a list of objects? If not, how can I implement one?
Desired result:
[
(new ABaseClass(...)),
(new ASubClass(...)),
(new ABaseClass(...)),
(new ASubClass(...)),
...
]
Environment details:
- CanJS: 1.17
- jQuery: 1.10.1
- I do not have control over the types of objects returned by the endpoint.
- Multiple AJAX calls are not an acceptable solution.