【问题标题】:Im trying to understand ember and ember cli conventions and file structure我试图了解 ember 和 ember cli 约定和文件结构
【发布时间】:2015-10-07 14:30:37
【问题描述】:

我已经使用 ember cli 设置了一个简单的 api 和一个新的 ember 应用程序

但是,虽然我可以获得完整的“项目”列表,但我无法使用参数让单个项目工作。

我的文件设置如下:

/models
  item.js
/routes
  /items
    index.js
    show.js
/templates
  /items
    index.hbs
    show.hbs
  application.hbs
  index.hbs
  items.hbs
router.js

作为参考,这是我在文件中的内容

我设置了我的 models/item.js 如下:

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string')
});

我将代码添加到我的 router.js

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
  this.route('items', function() {
    this.route('show', { path: '/:item_id' });
  });
});

export default Router;

然后我创建了我的 templates/index.hbs

{{outlet}}

最后,添加了我的 templates/items/index.hbs

<ul>
  {{#each model as |item|}}
      <li>
      {{#link-to 'items.show' item}}
        {{item.name}}
      {{/link-to}}
      </li>
  {{/each}}
</ul>

这按预期工作。

但是,当我单击链接 (/items/1) 时,它不再按预期执行。假设我不使用任何车把变量 ({{item.name}}),它将正确显示 items/show.hbs。例如,如果我只包含文本,this is my items/show.hbs 它将显示文本。当我添加时,{{name}} 它显示一个空白页

这是我的templates/items/show.hbs

{{name}}

还有我的routes/items/show.js

import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    return this.store.queryRecord('item', params.item_id);
  }
});

我做错了什么?

提前致谢!

【问题讨论】:

  • 模型(参数)?应该是模型:函数(参数)
  • 谢谢!我原来是这样的。我改回来了,还是什么都没有。
  • 控制台说什么,有什么错误吗?
  • 另外 {{name}} 应该参考模型 {{model.name}}
  • model(params) 很好(作为 ES6 语法的一部分)。

标签: ember.js ember-cli


【解决方案1】:

在模板中使用模型时,您似乎没有使用对模型的引用

这是我的模板/项目/show.hbs

{{name}} < This is a model's property- name 
Correct usage: {{model.name}}

还有我的路线/items/show.js

import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    return this.store.queryRecord('item', params.item_id);
  }
});

哈希的使用方式

            return Ember.RSVP.hash({
                something: store.find('y', { study: params.study_id }),
                somethingElse: store.find('x', 1),

            });

【讨论】:

  • 谢谢!!我一直在尝试 {{item.name}} 和 {{name}},但从未想过要使用 {{model.name}}。这解释了很多。
  • 它有助于更​​好地分离不同的数据类型。顺便说一句,我建议使用 Ember.RSVP.hash。 @RickyMason 更新了哈希使用的答案。然后你需要使用 model.something.name 来引用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
  • 2014-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多