【发布时间】:2026-01-27 02:15:01
【问题描述】:
我是 ember.js 的新手(但我从我的 CakePHP 经验和 JS 中了解 MVC) 在过去的两周里,我在工作中构建了一个加载的小应用程序 使用 REST-Adapter 侧载 JSON 数据。我添加了一些带有动作的按钮 一切都很好,我学到了很多东西,但要弄清楚细节需要时间。
我所有的控制器、路由和模型目前都没有组织在一个文件夹结构中 这在 ember.js-Guide 中有描述:http://guides.emberjs.com/v1.12.0/concepts/naming-conventions/
现在我想将文件移动到文件夹结构中。但随后该应用程序不起作用。 在我看来,解析器在运行时找不到文件。但为什么呢?
一个不起作用的简单示例:
在“app.js”中(在“index.html”中加载了-tag):
App = Ember.Application.create({
LOG_RESOLVER: true // just for debugging
});
在“router.js”中(在“index.html”中的“app.js”之后加载-tag):
App.Router.map(function(){
this.route('mytest');
});
在“路由/mytest.js”中:
export default Ember.Route.extend({
setupController: function(controller) {
controller.set('title', "Hello world!");
}
});
在“控制器/mytest.js”中:
export default Ember.Controller.extend({
appName: 'mytest'
});
在“indes.html”中:
<script type="text/x-handlebars">
{{#link-to 'mytest' }}Mytest{{/link-to}}<br/>
{{outlet}}
</script>
<script type="text/x-handlebars" id="mytest">
mytest Template
<h1>appName: {{appName}}</h1>
<h2>title: {{title}}</h2>
</script>
Handlebar 模板“mytest”已显示,但 {{appName}} 和 {{title}} 为空。 如果我将“mytest”-Template 移动到“templates/mytest.hbs”,则不会显示任何内容。
在我看来,解析器不起作用。还是命名约定指南有问题?
我的另一个假设: Resolver-Stuff 仅在使用 ember-cli 之类的构建过程时才有效。
使用“ember-1.12.1.debug”、“ember-template-compiler-1.12.1”
感谢您的帮助。
-更新- 这里是“index.html”——简单的例子:
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
</head>
<body>
<script type="text/x-handlebars">
{{#link-to 'mytest' }}Mytest{{/link-to}}<br/>
{{outlet}}
</script>
<script type="text/x-handlebars" id="mytest">
mytest Template
<h1>appName: {{appName}}</h1>
<h2>title: {{title}}</h2>
</script>
<script src="libs/jquery-2.1.4.js"></script>
<script src="libs/ember-template-compiler-1.12.1.js"></script>
<script src="libs/ember-1.12.1.debug.js"></script>
<script src="app.js" ></script>
<script src="router.js" ></script>
</body>
</html>
【问题讨论】:
-
如果你正在寻找一个干净的项目结构,你应该给ember-cli看看
-
能否提供完整的 index.html?看起来您期望的东西可以在 ember-cli 中使用的文件结构中工作,但没有 ember-cli?这真的行不通..
-
上周我查看了 ember-cli,但在使用过程中还有更多的魔力。例如:{{outlet}} 移动了,我不清楚 {{content-for ... }} 的魔法是如何工作的。并且有一个 标记,其中包含一个很长的配置字符串作为值。
标签: ember.js