15

With the recent update, I know that in routers and controllers, I can easily just do this.store.find('model'). However I have some functions that need to call find that are not in routers and controllers. So how can I get an instance store from anywhere in an Ember App?

I know worst comes to worst I can do App.__container__.lookup('store:main'), but I'll try to stay away from that.

HaoQi Li
  • 11,970
  • 14
  • 58
  • 77

4 Answers4

10

The TRANSITION doc says that you can do this to inject the store into components :

App.inject('component', 'store', 'store:main');

You might be able to change 'component' to 'view' or to 'model', but I'm not sure about that.

Jeremy Green
  • 8,547
  • 1
  • 29
  • 33
7

You can do

App.Model.store.find('model')

If you have a particular attribute to filter with, you can do:

App.Model.store.find('model', {'attribute_name' : 'matching_to_this_value'})

See more on this post.

Community
  • 1
  • 1
HaoQi Li
  • 11,970
  • 14
  • 58
  • 77
  • 1
    Just for clarity, that's if your model class is `Model`...or if your model was `Post`, then `App.Post.store.find('post')`. This is probably frowned upon but it works great for me! – S'pht'Kr Feb 09 '15 at 11:47
7

You can try to do

this.get('controller').get('store').find('model')

That would wok in a View for example.

Simon Cateau
  • 651
  • 4
  • 10
1

With Ember Data 1.0.0-beta.X:

App.YourModelType.store.find("yourModelType", someId).then( 
  function(modelInstance) { 
  ...
  });
Abdull
  • 26,371
  • 26
  • 130
  • 172