How to get Ember Data model store name

How can I determine the "store name" (not sure what the correct terminology is) for a given ED model? Let's say I have App.Payment , is there a repository method that allows me to search for its corresponding name, i.e. payment (for example, use in find queries)?

+7
ember-data
source share
4 answers

For Ember Data 1.0 (and later)

modelName is a dasherized string. It is stored as a property of the class, so if you have an instance of the model:

 var model = SuperUser.create(); console.log(model.constructor.modelName); // 'super-user' 

For Ember Data Pre 1.0

typeKey is the name of the model string. It is stored as a property of the model class, so if you have an instance of the model:

 var model = App.Name.create({}); console.log(model.constructor.typeKey); // 'name' 
+26
source share

Perhaps you are looking for the dsherize method for the Ember method:

 var fullClassName = "App.SomeKindOfPayment"; var className = fullClassName.replace(/.*\./, ""); // => "SomeKindOfPayment" var dasherizedName = Ember.String.dasherize(className); // "some-kind-of-payment" 

There may be a built-in way in Ember to do this, but I did not find it, having spent some time searching.

EDIT: Ember Data may also allow you to leave with the "App.SomeKindOfPayment" transfer when the model name is required - it usually checks the format of the model name and updates it to the required format itself.

0
source share

store.find , store.createRecord and other save methods use store.modelFor('myModel') . After some configuration, call container.lookupFactory('model:' + key); where the key is "myModel". Therefore, any valid factory query syntax is applicable. For example:

For a model called OrderItems you can use: order.items , order_items , order-items , OrderItems .

0
source share

It turns out there is no need to do this, and here's why:

I tried to provide a string representation of the model (" payment " for App.Payment ) to call store.findAll("payment") . However, looking at the ED source for the store , the findQuery function calls modelFor to search for the factory ( App.Payment ) from the string ( payment ), if only the factory is already provided. And the factory is easily accessible from the controller by calling this.get('model').type . There is no need to convert it to a string (and vice versa).

Here is the corresponding code from the Ember data source.

 modelFor: function(key) { var factory; if (typeof key === 'string') { factory = this.container.lookupFactory('model:' + key); Ember.assert("No model was found for '" + key + "'", factory); factory.typeKey = key; } else { // A factory already supplied. factory = key; } factory.store = this; return factory; }, 
-one
source share

All Articles