【问题标题】:Ember data from Mirage fails to display on index.hbs来自 Mirage 的 Ember 数据无法在 index.hbs 上显示
【发布时间】:2016-02-05 22:24:36
【问题描述】:

Ember 新手在这里。我一直在关注 Ember 网站上的教程here.

我一直在研发这个词的例子,一切正常……直到我尝试实现 Mirage。数据永远不会显示在 index.hbs 页面上。

这是我的模型钩子:

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return this.store.findAll('rental');
  },
});

还有我的模型:rental.js

import DS from 'ember-data';
export default DS.Model.extend({
  title: DS.attr('string'),
  owner: DS.attr('string'),
  city: DS.attr('string'),
  type: DS.attr('string'),
  image: DS.attr('string'),
  bedrooms: DS.attr('number')
});

我的 index.hbs:

<h1> Welcome to Super Rentals </h1>

We hope you find exactly what you're looking for in a place to stay.

{{#each model as |rentalUnit|}}
  {{rental-listing rental=rentalUnit}}
{{/each}}


{{#link-to "about"}}About{{/link-to}}
{{#link-to "contact"}}Click here to contact us.{{/link-to}}

最后是我的 app/mirage/config.js:

export default function() {
  this.get('/rentals', function() {
    return {
      data: [{
        type: 'rentals',
        id: 1,
        attributes: {
          title: 'Grand Old Mansion',
          owner: 'Veruca Salt',
          city: 'San Francisco',
          type: 'Estate',
          bedrooms: 15,
          image: 'https://upload.wikimedia.org/wikipedia/commons/c/cb/Crane_estate_(5).jpg'
        }
      }, {
        type: 'rentals',
        id: 2,
        attributes: {
          title: 'Urban Living',
          owner: 'Mike Teavee',
          city: 'Seattle',
          type: 'Condo',
          bedrooms: 1,
          image: 'https://upload.wikimedia.org/wikipedia/commons/0/0e/Alfonso_13_Highrise_Tegucigalpa.jpg'
        }
      }, {
        type: 'rentals',
        id: 3,
        attributes: {
          title: 'Downtown Charm',
          owner: 'Violet Beauregarde',
          city: 'Portland',
          type: 'Apartment',
          bedrooms: 3,
          image: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Wheeldon_Apartment_Building_-_Portland_Oregon.jpg'
        }
      }]
    };
  });
}

我在 Chrome 开发者控制台中收到两条消息:

Mirage:您的 Ember 应用尝试获取“http://localhost:4200/assets/vendor.js”,但没有定义路由来处理此请求。在 mirage/config.js 文件中定义与此路径匹配的路由。您忘记添加命名空间了吗?

还有这个警告:

警告:在负载中遇到“数据”,但没有找到模型名称“数据”的模型(使用 super-rentals@serializer:-rest:.modelNameFromPayloadKey("data") 解析模型名称)

但是,当我看到以下信息时,信息似乎已成功检索:

请求成功:GET /rentals 对象{数据:数组[3]}

这反映了正确的数据。它只是介于它和 index.hbs 之间的某个地方,我是新手才能弄清楚。我敢肯定这只是我的一个小误会。任何帮助将不胜感激!

【问题讨论】:

    标签: javascript ember.js ember-cli-mirage


    【解决方案1】:

    您安装了错误的 Ember Data 版本。还要确保在安装依赖项时重新启动服务器。

    您收到的错误消息意味着您的应用程序正在使用 RESTAdapter(Ember Data 1.x 的默认适配器)。这就是它查看顶级data 键并尝试单数化并找到相关模型的原因。

    您可以按照latest Ember CLI release 上的说明进行更新。或者,您可以npm install -g ember-cli@beta 并从头开始。

    【讨论】:

    • 我按照您提供的链接上的说明来全局更新 ember-cli 并从头开始项目。像魅力一样工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 2021-11-23
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多