最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ember Data: Get a Model in the Console - Stack Overflow

programmeradmin8浏览0评论

I have the simplest possible Ember app in this JSBin. All I'm trying to do is find a model. Based on other SO questions, I've tried the following.

App.User.get('store').find('user', 1);
App.User.Store.find('user', 1);

I've defined App.Store, but App.Store returns undefined in the console. I'm obviously missing the absolute most basic concepts of Ember models. Explain like I'm 5, please? I literally just want to return a user object and call a property on it.

I have the simplest possible Ember app in this JSBin. All I'm trying to do is find a model. Based on other SO questions, I've tried the following.

App.User.get('store').find('user', 1);
App.User.Store.find('user', 1);

I've defined App.Store, but App.Store returns undefined in the console. I'm obviously missing the absolute most basic concepts of Ember models. Explain like I'm 5, please? I literally just want to return a user object and call a property on it.

Share Improve this question edited May 23, 2017 at 12:18 CommunityBot 11 silver badge asked Nov 18, 2013 at 16:31 nickcoxdotmenickcoxdotme 6,6979 gold badges48 silver badges72 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 12

The store is injected to routes/controllers. Inside a route/controller you can use this.store.find('user', 1) to fetch a model. But there is no global access to the store.

http://jsbin./aYIkAcUk/6/edit

If you feel guilty about touching the innards, which you should a bit, do not read on.

For transparency sake, you can get the store globally. After the application has initialized and is ready you can use the innards of ember to fetch it using the container lookup method. (If you need to know when the app is ready you can use the application ready hook.

 var store = App.__container__.lookup('store:main');
 var user = store.find('user', 1);

http://jsbin./aYIkAcUk/7/edit

As of Ember Data 2, the data store is now handled as a service. Thus, to look it up globally, the statement is now:

App.__container__.lookup('service:store')

Where App is the name of your Ember application. Kingpin2k's answer will not work with Ember Data 2.

发布评论

评论列表(0)

  1. 暂无评论