我是 ember.js 新手,通过检查 ember.js 应用程序的代码库来学习它。
这是一个博客应用程序,在路由 /routes/posts.js
中,代码从 Controller 获取两个计算属性,
var currentPost = this.get('controller.currentPost');
var posts = this.get('controller.sortedPosts');
我知道 ember.js 推断了很多东西,所以我假设这个 controller
指的是 Controller /controllers/posts.js
,我查看并找到了定义属性sortedPosts
,但是没有属性currentPost
的定义,我插入了一行代码console.log(currentPost)
,我可以看到浏览器开发工具的控制台选项卡中输出了正确的值,但我无法弄清楚该属性的定义位置。
我试图在 ember.js 官方文档中找到答案,但没有成功。 我的问题是
- ember.js 确实通过文件名将 Controller 推断为
/controllers/posts.js
- ember 是否足够智能,可以自动将模型加载为
currentPost
谢谢。
最佳答案
1) 是的,如果该文件存在,ember 会将 Controller 推断为 /controllers/posts.js
。
2) Current Post 是 Controller 的计算属性,它不会将模型加载为 currentPost
。
注意:Ember Controller 正在被贬值,取而代之的是可路由组件 - 因此,对于新应用程序,建议不惜一切代价避免使用 Controller 。
有关此问题和其他 future 验证问题的更多信息,请参阅:https://gist.github.com/samselikoff/1d7300ce59d216fdaf97
关于javascript - 从 ember.js 中的 Controller 检索计算属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33605146/