Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Ember Data's "store" from anywhere in the application so that I can do store.find()?

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.

like image 512
HaoQi Li Avatar asked Sep 12 '13 01:09

HaoQi Li


People also ask

How does Ember data work?

Ember Data is a model library that manages finding data, making changes, and saving it back to the server. It's a bit like an ORM in that it abstracts the underlying persistence mechanism behind an easy to use API. If you're familiar with using ORMs, you will find Ember Data easy to work with.

What is store in Ember?

The store contains all of the data for records loaded from the server. It is also responsible for creating instances of Model that wrap the individual data for a record, so that they can be bound to in your Handlebars templates. Define your application's store like this: app/services/store.js.


4 Answers

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.

like image 135
Jeremy Green Avatar answered Oct 19 '22 16:10

Jeremy Green


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.

like image 41
HaoQi Li Avatar answered Oct 19 '22 14:10

HaoQi Li


You can try to do

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

That would wok in a View for example.

like image 33
Simon Cateau Avatar answered Oct 19 '22 16:10

Simon Cateau


With Ember Data 1.0.0-beta.X:

App.YourModelType.store.find("yourModelType", someId).then( 
  function(modelInstance) { 
  ...
  });
like image 44
Abdull Avatar answered Oct 19 '22 15:10

Abdull