【发布时间】:2014-09-01 09:29:28
【问题描述】:
我对 EmberJS 还是很陌生。尝试制作一些简单的应用程序来熟悉它。有一种行为我不太了解。我正在 ember-cli 上构建它。
路由器
Router.map(function() {
this.resource('quotes', {path: '/'}, function() {
this.route('new');
this.resource('quote', {path: '/quotes/:id'}, function() {
this.route('delete');
});
});
});
routes/quote.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
return this.store.find('quote', params.id);
}
});
controllers/quote.js
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
deleteQuote: function() {
// is calling this.model here OK?
this.model.destroyRecord();
this.transitionTo('quotes');
}
}
});
quotes.hbs
{{#link-to 'quotes.new' tagName='h3' class='new-quote'}}New Quote{{/link-to}}
<ul>
{{#each quote in controller}}
{{#link-to 'quote' quote tagName='li' class='quotes'}}
{{quote.title}}
{{/link-to}}
{{/each}}
</ul>
{{outlet}}
quote.hbs
{{!-- probably shouldn't be referring to model directly here... --}}
<p class='description'>{{model.body}}</p>
<button {{action deleteQuote}}>Delete Quote</button>
当我明确定义一个 QuoteController 时,我只需要在报价模板中使用“model.body”。但是,如果我不制作 controllers/quote.js 文件,我可以在报价模板中使用不带模型前缀的 body。
我不确定这是否是 Ember 中的标准,但不知何故,我似乎在模板中调用 model.something。如果有人可以向我解释这种逻辑/行为以及这样做的“正确”方法是什么,将不胜感激。提前非常感谢!
[编辑]
感谢 BenjaminRH 提供的解决方案。对于其他正在查看该问题并想了解更多信息的人,以下内容可能会有所帮助:http://coryforsyth.com/2014/02/17/ember-controller-versus-objectcontroller/
【问题讨论】:
标签: ember.js