在param路线上加载模型

时间:2018-02-20 17:12:01

标签: ember.js ember-data

我已成功构建我的应用程序以获得正确的子路径,并且当用户浏览网站时它可以正常工作,但是在直接加载到show route(exhibitions / slug)时,ember数据似乎打破了;但是,我仍然从API获得适当的响应。这是我的代码,希望它有意义..

路由/展

body.modal-open {
    overflow: visible;
}

路由/展

import Route from '@ember/routing/route';

const { set } = Ember;

export default Route.extend({
  model() {
    return this.store.findAll('exhibition');
  },
  setupController(controller, model) {
    set(controller, 'exhibitions', model);
  }
});

展览模型

import Ember from 'ember';

const { get, set } = Ember;

export default Ember.Route.extend({
  model(params) {
    return this.store.queryRecord('exhibition', {
      'fields.slug': params.exhibition_slug
    });
  },
  serialize(model) {
    return { exhibition_slug: get(model, 'slug') };
  },
  setupController(controller, model) {
    set(controller, 'exhibition', model);
  }
});

router.js

import Contentful from 'ember-data-contentful/models/contentful';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';

export default Contentful.extend({
  title: attr('string'),
  body: attr('string'),
  slug: attr('string')
});

这是JSON响应(格式不佳的道歉)

import EmberRouter from '@ember/routing/router';
import config from './config/environment';

const Router = EmberRouter.extend({
  location: config.locationType,
  rootURL: config.rootURL
});

Router.map(function() {
  this.route('homepage', { path: '/' });
  this.route('exhibitions');
  this.route('exhibition', { path: 'exhibitions/:exhibition_slug'});
});

export default Router;

我在控制台中收到的错误是:

{sys: {type: "Array"}, total: 1, skip: 0, limit: 1, items: [{,…}]}
items
:
[{,…}]
0
:
{,…}
fields
:
{title: "Hello", body: "This is the body.", slug: "hello"}
body
:
"This is the body."
slug
:
"hello"
title
:
"Hello"
sys
:
{space: {sys: {type: "Link", linkType: "Space", id: "kw98h7kialqf"}}, id: "5rllAHKFygCOma2wgy6WuI",…}
contentType
:
{sys: {type: "Link", linkType: "ContentType", id: "exhibition"}}
createdAt
:
"2018-02-20T15:48:08.093Z"
id
:
"5rllAHKFygCOma2wgy6WuI"
locale
:
"en-US"
revision
:
2
space
:
{sys: {type: "Link", linkType: "Space", id: "kw98h7kialqf"}}
type
:
"Entry"
updatedAt
:
"2018-02-20T15:52:19.063Z"
limit
:
1
skip
:
0
sys
:
{type: "Array"}
type
:
"Array"
total
:
1

如果需要其他任何内容,请告诉我!!谢谢!

0 个答案:

没有答案