【问题标题】:EmberJS / ember-cli - how to not refer to model in the template?EmberJS / ember-cli - 如何不在模板中引用模型?
【发布时间】: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


    【解决方案1】:

    尝试将其设为ObjectController 而不是Controller。这是Ember guide了解更多详情。

    【讨论】:

    • 做到了!谢谢。我想我明白了。
    猜你喜欢
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多