Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember Data: model.get('modelName') is undefined, but model._internalModel works

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?

like image 498
Max Wallace Avatar asked Jan 18 '16 21:01

Max Wallace


1 Answers

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.

like image 55
locks Avatar answered Sep 26 '22 15:09

locks