我无法使 Ember.js 网站指南部分的第二个代码示例正常工作。正在关注the Simple Routes example似乎没有达到最终 Web 应用程序的预期目的。
我已遵循从指南开始到该示例结束的所有步骤,准确复制代码,但有两个异常(exception)。首先,我对 routes/favorites.js
做了一个小更改,以解决缺少服务器后端的问题,如下所示:
// import ajax from 'ic-ajax';
export default Ember.Route.extend({
model() {
// // the model is an Array of all of the posts
// // fetched from this url
// return ajax('/a/service/url/where/posts/live');
return [{ title: 'Test 1' }, { title: 'Test 2' }];
}
});
其次,我向 templates/application.hbs
添加了 {{outlet}}
来显示 templates/favorites.hbs
:
<h1>{{appName}}</h1>
<h2>{{model.title}}</h2>
{{outlet}}
不幸的是,运行 emberserve
时转到 /favorites
只会显示与 /
相同的内容:应用程序的内容。 hbs
(不含 favorites.hbs
的内容)。我希望这会显示一个包含项目“测试 1”和“测试 2”的列表。
为什么这不起作用?我做错了什么吗?
当我在命令行上运行 ember -v
时,我得到:
version: 1.13.6
node: 0.12.7
npm: 2.13.2
os: darwin x64
更新:这是templates/favorites.hbs
:
<ul>
{{#each controller as |item|}}
<li>{{item.title}}</li>
{{/each}}
</ul>
这里是/router.js
:
var Router = Ember.Router.extend();
Router.map(function(){
this.route('favorites');
});
export default Router;
我从服务器收到弃用警告:
DEPRECATION: Using `{{controller}}` or any path based on it ('my-app/templates/favorites.hbs' @ L2:C0) has been deprecated.
我还收到四个 JSHint 错误,如下所示:
'Ember' is not defined.
最佳答案
好吧,这并不完全明显。基本上 ember-cli 有一个禁用代理 Controller 的软件包:https://github.com/cibernox/ember-disable-proxy-controllers
这与:
DEPRECATION: Using
{{controller}}
or any path based on it ('my-app/templates/favorites.hbs' @ L2:C0) has been deprecated.
当您像指南建议的那样使用 ember-cli 时,您实际上无法迭代 Controller 。相反,迭代 model
(这就是您的数据未呈现的原因):
{{#each model as |item|}}
<li>{{item.title}}</li>
{{/each}}
要摆脱:
'Ember' is not defined.
将其添加到正在引发的文件的顶部:
import Ember from 'ember';
关于javascript - 为什么 Ember.js 指南中的这个示例不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31778253/