Ember数据:在控制台中获取模型 [英] Ember Data: Get a Model in the Console
问题描述
我在这个 JSBin 中拥有最简单的Ember应用程序。所有我想要做的是找到一个模型。根据其他 SO问题,我尝试过以下。
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);
我已经定义了 App.Store
但是在控制台中 App.Store
返回 undefined
我显然错失了绝对最基本的概念Ember模型。解释一下我5点呢?我真的只想返回一个用户
对象并调用一个属性。
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.
推荐答案
商店注入路由/控制器。在路由/控制器中,您可以使用 this.store.find('user',1)
来获取模型。但是没有全球访问商店。
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.com/aYIkAcUk/6/edit
如果你感到内疚,你应该有点内疚,不要阅读。
为了透明度,您可以在全球获取商店。在应用程序初始化并准备就绪后,您可以使用余烬内部使用容器查找方法来获取它。 (如果您需要知道应用程序准备就绪,可以使用应用程序准备好
钩子。
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。 com / aYIkAcUk / 7 / edit
这篇关于Ember数据:在控制台中获取模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!