samedi 31 janvier 2015

Ember-Data Embedded Polymorphic BelongsTo not working

I've been trying to get polymorphic associations working for me in Ember Data, but I haven't had any luck yet. Hopefully someone can have a look and let me know if I'm missing something.


I'm using Ember 1.8.1 and Ember-Data 1.0.0-beta.14.1. Also, we are using the ECMAScript6 syntax (import, export default).


Basically, we just have one embedded polymorphic relationship that doesn't seem to be working for us. If anyone has any suggestions or ideas about why this isn't working, I'd love some help. I've been stuck on this one for a few days now.


That's basically what we are having trouble with. The error that I get returned is:

Error: Assertion Failed: You can only add a 'metadata' record to this relationship





// /app/models/metadata.js:
export default DS.Model.extend({
type: DS.attr('string'),
lastModified: DS.attr('number');
});

// /app/models/metadata-story.js
import Metadata from '/app/models/metadata';
export default Metadata.extend({
body: DS.attr('string')
});

// /app/models/metadata-photo.js
import Metadata from '/app/models/metadata';
export default Metadata.extend({
url: DS.attr('string'),
width: DS.attr('number'),
height: DS.attr('number')
});

// /app/models/post.js
export default DS.Model.extend({
headline: DS.attr('string'),
metadata: DS.belongsTo('metadata', {polymorphic: true})
});


// /app/serializers/post.js
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin,{
attrs: {
metadata: {embedded: 'always'}
}
});


// /app/routes/posts.js
// Here is the code that is being run to load the records:
export default Em.Route.extend({
model: function(params){
var query = {
start: params.start,
offset: params.offset
};
return this.store.find('post', query);
}
});


// Also, here is a sample of the Payload that the normalizePayload function is returning:
{posts:[
{
id: 0,
headline: 'Well, that is SOME story!',
metadata: {
body: 'This is the body...'
},
metadataType: 'metadata-story'
},
{
id: 1,
headline: 'Well Hello',
metadata: {
url: 'http://ift.tt/16eiYus',
width: 450,
height: 350
},
metadataType: 'metadata-story'
}
]}

/*
Another thing that I should mention is that I have tried setting the Ember.MODEL_FACTORY_INJECTIONS flag to both true and false, but it has not seemed to make any difference at this point.
*/






Aucun commentaire:

Enregistrer un commentaire