0
votes

I'm using Ember version 1.0.0 in combination with Ember Data v1.0.0-beta.3. The problem is that I can't seem to get a valid reference to a model when adding some additional properties to the relationship. I'll give an example that shows what I want to accomplish, as I find it hard to explain.

First, my models are wired this way:

App.Employee = DS.Model.extend({
  firstName: DS.attr("string"),
  lastName: DS.attr("string"),
  languages: DS.hasMany("language", { async: true })
});

App.Language = DS.Model.extend({
  name: DS.attr("string"),
  employee: DS.belongsTo("employee")
});

Currently, I'm using fixtures, but later on I'm planning to use the RESTAdapter. This is what my JSON looks like:

App.Employee.FIXTURES = [
  {
    id: 1,
    firstName: "Alpha",
    lastName: "Dog",
    languages: [1, 2]
  },

  {
    id: 2,
    firstName: "Bravo",
    lastName: "Cat",
    languages: [3]
  }
];

App.Language.FIXTURES = [
  {
    id: 1,
    name: "German"
  },
  {
    id: 2,
    name: "Spanish"
  },
  {
    id: 3,
    name: "Russian"
  }
];

Now this works, but what I want to do is add additional information to the languages key in the Employee model. Like so, replacing the languages property with:

languages: [
  {
    id: 1,
    knowledge: 3
  },
  {
    id: 2,
    knowledge: 2
  }
]

I add an extra knowledge field about a language, and the id specified must refer to the id in the Language model. Running this gives me a TypeError (cannot call method 'toString' of undefined).

So how do I model/fix this? Can this be accomplished using polymorphic relationships or serializers, as I've come across these terms? I've looked for a long time now and this (new) framework gets me more and more confused.

1

1 Answers

0
votes

It looks like you need a mapping table (model) between employee and language where you can store the knowledge. Like so:

App.Employee = DS.Model.extend({
  firstName: DS.attr("string"),
  lastName: DS.attr("string"),
  languageSkills: DS.hasMany("languageSkill", { async: true })
});

App.LanguageSkill = DS.Model.extend({
  employee: DS.belongsTo("employee"),
  language: DS.belongsTo("language"),
  knowledge: : DS.attr('number'),
});

App.Language = DS.Model.extend({
  name: DS.attr("string"),
  languageSkills: DS.hasMany("languageSkill")
});