7

How can I determine the "store name" (not sure what the proper terminology is) for a given ED Model? Say I have App.Payment, is there a store method that let's me look up its corresponding name, i.e. payment (for example to use in find queries)?

chopper
  • 6,649
  • 7
  • 36
  • 53
  • You can call the store from every controller/router you are in. So it is not necessarily attached to a route or function. To get the current route that relates to your store name, look here: http://stackoverflow.com/questions/18302463/get-current-route-name-in-ember – DelphiLynx Nov 21 '13 at 11:21

4 Answers4

26

For Ember Data 1.0 (and later)

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

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

For Ember Data Pre 1.0

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

var model = App.Name.create({});
console.log(model.constructor.typeKey); // 'name'
sandstrom
  • 14,554
  • 7
  • 65
  • 62
typeoneerror
  • 55,990
  • 32
  • 132
  • 223
0

You might be looking for Ember's string dasherize method:

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

There might be a built-in way to do this in Ember, but I haven't found it after spending some time looking.

EDIT: Ember Data might also let you get away with passing "App.SomeKindOfPayment" when a model name is needed - it usually checks the format of the model name and updates it to the required format by itself.

Andy Pye
  • 301
  • 1
  • 9
0

store.find, store.createRecord, and other persistence methods, use the store.modelFor('myModel'). After some setup it call container.lookupFactory('model:' + key); where key is the 'myModel'. So any valid factory lookup syntax is applicable. For example:

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

Marcio Junior
  • 19,078
  • 4
  • 44
  • 47
-1

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

I was trying to the the string representation of the model ("payment" for App.Payment) in order to call store.findAll("payment"). However, looking at the ED source for store, the findQuery function calls modelFor to look up the factory (App.Payment) from the string (payment), unless a factory is already provided. And the factory is easily accessible from the controller by calling this.get('model').type. There's no need to convert it to a string (and back).

Here's the relevant 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;
  },
chopper
  • 6,649
  • 7
  • 36
  • 53
  • This doesn't actually answer the question you posed (unlike another answer), though the information is relevant. – Alex Apr 07 '15 at 14:37