One of the core powers of DataFox is our ability to show you a customized feed of recent events affecting companies you care about. For example, today I can see that my former employer, Box, launched a new product, while my friend’s company, FireEye, just announced a new partnership with Thomson Reuters.
My news feed on 3/7/17
When it comes to displaying an event in our app we create a new Ember data model:
and my template would look like:
But what if I want each event to have different display data and behavior (which they do)?
This is a classic case of polymorphism: we need a base
Event class for the common behavior
and lots of subclasses like
CompanyAcquisitionEvent that define
the specific data and actions of that event type. Ember doesn’t explain how to handle this
common case, so what do we do?
Fortunately, a friend in the Ember core team pointed us to an undocumented
in Ember data that does exactly what we need:
The corresponding JSON response looks like:
Ember data automatically wires up the
type fields so the
event.object points directly to
CompanyAcquisition model. And we can update our generic event template to include
specific rendering for each event by adding a
template variable to each model we define*:
* Normally it would be incorrect to define a view (the template) in a model under MVC, but Ember’s actually follows MVVM, so it’s a bit confusing. In my judgment, this is the clearest solution.