dimanche 29 janvier 2017

You need to pass a model name to the store's modelFor method

I have problem with making hasMany <=> belongTo relationship to work.

I have articles/show view, when I try to list article's comments but I keep recieving the error pointed in the title.

It's something with belongsTo: DS.belongsTo('article') but I couldn't figure out what it is.

Here are my files.

routes/articles/show.js

import Ember from 'ember';
import RSVP from 'rsvp';

export default Ember.Route.extend({
  model(params) {
    return RSVP.hash({
      article: this.store.find("article", params.id),
      comments: this.store.query('comment', { articleId: params.id })
    });
  }
});

controllers/articles/show.js

import Ember from 'ember';

const { computed: { alias, readOnly } } = Ember;

export default Ember.Controller.extend({
  article: alias('model.article'),
  comments: alias('model.comments'),
  length: readOnly('comments.length')
});

templates/articles/show.hbs

<h3>Comments ()</h3>

  <p>Author: </p>
  <p>Somebody said: </p>


adapters/comment.js

import ApplicationAdapter from './application';
export default ApplicationAdapter.extend({});

serializers/comment.js

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
  attrs: {
    user: { embedded: 'always' },
    article: { embedded: 'always' }
  }
});

serializers/article.js

import DS from 'ember-data';

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    comments: { embedded: 'always' }
  }
});

models/article.js

import DS from 'ember-data';
import Ember from 'ember';

const { attr, hasMany } = DS;
const { computed: { gt } } = Ember;

export default DS.Model.extend({
  title:        attr('string'),
  content:      attr('string'),

  authorName:   attr('string'),
  authorAvatar: attr('string'),
  authorUrl:    attr('string'),

  comments:     hasMany('comment', {async: true}),

  hasAvatar: gt('authorAvatar.length', 0)
});




Aucun commentaire:

Enregistrer un commentaire