2

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

我一直在研发这个词的例子,一切正常……直到我尝试实现 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 Object {data: Array[3]}

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

4

1 回答 1

1

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

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

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

于 2016-02-06T00:06:32.493 回答