Polymorphism & ember.js data HowTo?

Some example code using com.fasterxml jackson

Animal.java:

@Entity
@Table
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="type")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type")
@JsonSubTypes({
    @JsonSubTypes.Type(value=Animal.class, name="animal"),
    @JsonSubTypes.Type(value=Dog.class, name="dog"),
    @JsonSubTypes.Type(value=Cat.class, name="cat"),
})
public class Animal {
     @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
     
    @JsonProperty
    private int a;
}

Dog.java:

@Entity
public class Dog extends Animal {
    @JsonProperty
    private int d;
}

Cat.java:

@Entity
public class Cat extends Animal {
    @JsonProperty
    private int c;
}

RestSerivce.java:

@JsonRootName("animals")
public class Animals extends ArrayList<Animal> {
private static final long serialVersionUID = 1L;

 public Animals(Collection<? extends Animal> c) {
    addAll(c);
 }
}

@GET
@Override
@Produces({"application/xml", "application/json"})
public Animals findAll() {
    Animals animals = new Animals(getListAnimalsFromSomWhere());
    return animals;
}

Calling getAnimals will return something like

{"animals":[{"type":"animal","id":6,"a":0},{"type":"cat","id":8,"a":2,"c":22},{"type":"dog","id":7,"a":1,"d":11}]}

Can ember data (howto?) create a Animal, Dog & Cat object of this using type property or only animal objects?

I’m not sure if this is what you meant, but you can declare classes that use mixins. I’m pretty sure you can create a mixin and use it when declaring models.

App.FlyingMixin = Object.Mixin.create({
    fly: function() {
        console.log('flying')
    },
    height: DS.attr()
});

App.BarkingMixin = Object.Mixin.create({
    bark: function() {
       console.log('bark');
    },
    aggression: DS.attr()
});

App.AggressiveFlyingAnimal = Ember.Model.extend( [App.FlyingMixin, App.BarkingMixin], {
    type: DS.attr()
});

I would be surprised if this didn’t work.