I'm using Ember Data 2.2.0 and Ember 2.2.1. After retrieving a model from the data store, I'd like to get the model's type name as a string.
According to the API Docs, DS.Model defines a modelName property, which looks like what I want. However, I find that model.modelName
, and model.get('modelName')
are undefined, after retrieving model
from the store with findRecord
.
On the other hand, model._internalModel.modelName
returns the lowercased, dasherized name of the model, as expected.
What's going on here?
It looks like a slight ambiguity in the documentation. It doesn't help that the modelName
example uses DS.Store#modelFor
which could be confused with Ember.Route#modelFor
.
DS.Store#modelFor
returns, according to the documentation, "a model class for a particular key. Used by methods that take a type key (like find, createRecord, etc.)". What I understand by this is that it returns the actual DS.Model
class, and not the instance. _internalModel
also returns the DS.Model
class, hence the same behaviour.
I believe the safer alternative is doing model.constructor.modelName
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With