mercredi 28 janvier 2015

How do I declare a `type` key on a polymorphic relationship in Ember.js?

I'm trying to learn ember.js and I have a problem with trying to use the DS.FixtureAdapter with mock data. Whenever I try to access the store I get an Ember error of:


Error while processing route: exercises Assertion Failed: Ember Data expected a number or string to represent the record(s) in the pictureExercises relationship instead it found an object. If this is a polymorphic relationship please specify a type key. If this is an embedded relationship please include the DS.EmbeddedRecordsMixin* and specify the pictureExercises property in your serializer's attrs hash.


Looking at the ember data section in the ember inspector I can see all the top level exercise data is loaded correctly, the PictureExercises and VideoExercises are not.


How do I declare a type key on a polymorphic relationship in Ember.js?


I'm at a total loss and any help would be greatly appreciated. Thanks for your time.



//Create application store
Gymbo.ApplicationAdapter = DS.FixtureAdapter;

//Set up models and relationships
Gymbo.Exercise = DS.Model.extend({
//id: DS.attr("number"), /*Ember will error if id is in model* /
name: DS.attr("string"),
description: DS.attr("string"),
pictureExercises: DS.hasMany("pictureExercise", {async: true }),
videoExercises: DS.hasMany("videoExercise", { async: true })
});


Gymbo.PictureExercise = DS.Model.extend({
//id: DS.attr("number"),
exerciseId: DS.attr("number"),
mimeType: DS.attr("string"),
picFilePath: DS.attr("string"),
picName: DS.attr("string"),
exercise: DS.belongsTo("exercise")
});

Gymbo.VideoExercise = DS.Model.extend({
//id: DS.attr("number"),
exerciseId: DS.attr("number"),
videoName: DS.attr("string"),
videoFilePath: DS.attr("string"),
videoType: DS.attr("string"),
exercise: DS.belongsTo("exercise")
});

//UsersRoute
Gymbo.ExercisesRoute = Ember.Route.extend({
model: function () {
return this.store.find("exercise");
}
});

//MOCK DATA
Gymbo.Exercise.reopenClass({
FIXTURES:
[
{
"id": 101,
"name": "Exercise 1",
"description": "Description here",
"pictureExercises": [
{
"id": 106,
"exerciseId": 101,
"mimeType": "PNG",
"picFilePath": "images\\strength",
"picName": "s001"
},
{
"id": 107,
"exerciseId": 101,
"mimeType": "JPG",
"picFilePath": "images\\strength",
"picName": s002
}
],
"videoExercises": [
{
"id": 101,
"exerciseId": 101,
"videoName": "Test",
"videoFilePath": "Video\\strength",
"videoType": "mp4"
}
]
},
{
"id": 102,
//Data removed for brevity.....
}
]
});




Aucun commentaire:

Enregistrer un commentaire