Ember-data fixtures adapter not loading all data - ember.js

I have an ember-data model definition that looks like this:
Sylvius.Filter = DS.Model.extend({
title: DS.attr('string'),
slug: DS.attr('string'),
// Belongs to Atlas
atlas: DS.belongsTo('Sylvius.Atlas'),
// Has images
images: DS.hasMany('Sylvius.Image'),
// May have AtlasExtras
extras: DS.hasMany('Sylvius.AtlasExtra'),
// Structures for this filter
structures: DS.hasMany('Sylvius.Structure'),
// This is the path to the thumbnails sprite.
// Each image will have an index on this sprite
thumbnailUrl: DS.attr('string'),
// How big is each thumbnail?
thumbnailHeight: DS.attr('number'),
thumbnailWidth: DS.attr('number'),
// How big are the images?
imageHeight: DS.attr('number'),
// which image is selected?
selectedImage: DS.belongsTo('Sylvius.Image')
});
I have an ember-data fixture-adapter store set up like this:
Sylvius.fixtureStore = DS.Store.create({
revision: 4,
adapter: DS.fixtureAdapter
});
...and fixtures which look like this:
Sylvius.Filter.FIXTURES = [{
"id": 1,
"title": "Unlabeled",
"slug": "unlabeled",
"thumbnailUrl": "assets/img/surface_anatomy/photographic/srf-photo-unlabeled-tn.gif",
"thumbnailWidth": 100,
"thumbnailHeight": 75,
"atlas_id": 1,
"images": [1, 2, 3, 4, 5, 6, 7],
"structures": [0]
}];
(All this code is in this jsfiddle which demonstrates the problem.)
Here's the issue: the title is accessible just fine. The slug is also there. The thumbnailUrl, thumbnailWidth, thumbnailHeight, are all undefined. Why?

You are not following ember-data's rails centric naming conventions. You can either change your fixture data to:
{
"id": 1,
"title": "Dummy Title",
"slug": "dummy-title",
"thumbnail_url": "path/to/thumbnail.gif",
"thumbnail_width": 100,
"thumbnail_height": 75,
"atlas_id": 1,
"images": [1, 2, 3, 4, 5, 6, 7],
"structures": [0]
}
or change your mapping to include a key:
thumbnailUrl: DS.attr('string', { key: 'thumbnailUrl' }),
thumbnailHeight: DS.attr('number', { key: 'thumbnailHeight' }),
thumbnailWidth: DS.attr('number', { key: 'thumbnailWidth' })

Related

HasMany Polymorphic Relationship In Ember Data

I'm really struggling to understand how polymorphic relationships worm in Ember Data (Beta 11) and cannot find any update information on how to set them up and what is expected in the JSON payload. I'm trying to create a feed of items (think facebook feed) where you have different types of items in the feed. My modeling looks something like the following.
App.Feedable = DS.Model.extend({
activities: DS.hasMany('activity')
});
App.Activity = DS.Model.extend({
feedable: DS.belongsTo('feedable', { polymorphic: true, async: false })
});
App.MemberLikeShare = DS.Model.extend({
status: DS.attr('string')
});
App.PhotoShare = DS.Model.extend({
status: DS.attr('string'),
photo: DS.attr('string')
});
When I do a fetch at /activities I send back JSON that looks like the following:
{
activities: [
{
id: 1,
feedable: { id: 1, type: 'memberLikeShare' }
},
{
id: 4,
feedable: { id: 4, type: 'memberLikeShare' }
},
{
id: 5,
feedable: { id: 5, type: 'photoShare' }
}
],
member_like_shares: [
{
id: 1,
status: 'Foo'
},
{
id: 4,
status: 'Bar'
}
],
photo_shares: [
{id: 5, photo: 'example.jpg'}
]
}
When this runs I get an error like:
You can only add a 'feedable' record to this relationship Error: Assertion Failed: You can only add a 'feedable' record to this relationship
I'm assuming my relationships are wrong or I'm sending the wrong JSON?
polymorphic relationships should extend the base type.
App.Feedable = DS.Model.extend({
activities: DS.hasMany('activity')
});
App.MemberLikeShare = App.Feedable.extend({
status: DS.attr('string')
});
App.PhotoShare = App.Feedable.extend({
status: DS.attr('string'),
photo: DS.attr('string')
});
I'd also expect them to define the activities on them.
member_like_shares: [
{
id: 1,
status: 'Foo',
activites: [1,2,3,4]
},
{
id: 4,
status: 'Bar',
activites: [1,2,3,4]
}
],
photo_shares: [
{
id: 5,
photo: 'example.jpg',
activites: [1,2,3,4]
}
]

How do I create a polymorphic 1:1 relationships wtih Ember Data fixtures?

Page has a polymorphic 1:1 relationship with a model called PageContent. PageContent has two subtypes (TextOnly and Video). I want to be able to able to do a findAll for "page" and get all of the content back. What am I doing wrong?
JSBin
This seems to work: http://jsbin.com/names/1/edit
Only wrong thing I could see is the App.Page.FIXTURES.
It should be:
App.Page.FIXTURES = [
{
id: 1,
title: "Introduction",
pageContent: 1,
pageContentType: "textOnly"
},{
id: 2,
title: "Summary",
pageContent: 1,
pageContentType: "Video"
}
];
or
App.Page.FIXTURES = [
{
id: 1,
title: "Introduction",
pageContent: {
id: 1,
type: "textOnly"
}
},{
id: 2,
title: "Summary",
pageContent: {
id: 1,
type: "Video"
}
}
];

creating new record in store with hasMany relationship

Using the following model and store, I can successfully load data using FIXTURES as shown below.
App.Item = DS.Model.extend({
itemName: DS.attr('string'),
strategy: DS.belongsTo('strat')
});
App.Strat = DS.Model.extend({
stratName: DS.attr('string'),
items: DS.hasMany('item',{async:true})
App.Store = DS.Store.extend({
adapter: DS.FixtureAdapter
});
App.StratLeg.FIXTURES =
[
{id: 1, itemName: 'I1', strategy: 1},
{id: 2, itemName: 'I2', strategy: 2},
{id: 3, itemName: 'l3', strategy: 2},
];
App.Strat.FIXTURES =
[
{id: 1, stratName: 's1', items: [1]},
{id: 2, stratName: 's2', items: [2,3]}
];
But when I tried to add a new record using javascript, I ran into all sorts of errors. Following the examples in the EmberData-API documention for DS.store, I tried:
var pushData = {
strat: [{id: 100, stratName: "s5", items: [101]}],
item: [{id: 101, itemName: "I5", strategy: 100}]};
this.store.push ('strat', pushData);
This generated the following error:
"you must include an 'id' in a hash passed to 'push'.
I've also tried various incarnations of store.createRecord, which resulted in different errors.
What's the proper way of doing this?
Depending on if you are in the latest version of ember data, you can use pushPayload and push them in this format (not the pluralized format of the keys)
var pushData = {
strats: [{id: 100, stratName: "s5", items: [101]}],
items: [{id: 101, itemName: "I5", strategy: 100}]
};
store.pushPayload('strat', pushData);
http://emberjs.jsbin.com/OHUcIx/1/edit

Retrieving ember fixture model via "needs"

Doing a basic update to an Ember Fixture record using the new "needs" helper instead of the old "controllerFor". Here is a distilled version of my code:
App.Item = DS.Model.extend({
utensil: DS.belongsTo('App.Utensil')
});
App.Utensil = DS.Model.extend({
name: DS.attr('string')
});
App.Item.FIXTURES = [
{id: 0, utensil: 2},
{id: 1, utensil: 1}
];
App.Utensil.FIXTURES = [
{id: 0, name: 'test1'},
{id: 1, name: 'test2'},
{id: 2, name: 'test3'},
{id: 3, name: 'test4'}
];
App.UtensilController = Ember.ArrayController.extend({
needs: ["item"],
setUtensil: function(utensil) {
var item_controller = this.get('controllers.item');
item_controller.get('model').set('utensil', utensil);
}
});
It seems that the item model is coming back empty. Is this is a fixture limitation or am I doing something wrong?

Ember-Data recursive hasMany association

Has anyone used ember-data to model a tree of data?
I would assume it would be something like:
Node = DS.Model.extend({
children: DS.hasMany(Node),
parent: DS.belongsTo(Node)
});
However, I have not been able to get this working which leads be to believe that either: 1) I'm just plain wrong in how I'm setting this up or, 2) it is not currently possible to model a tree using ember-data.
I'm hoping that it's the former and not the latter...
Of course it could be the JSON...I'm assuming the JSON should be of the form:
{
nodes: [
{ id: 1, children_ids: [2,3], parent_id: null },
{ id: 2, children_ids: [], parent_id: 1 },
{ id: 3, children_ids: [], parent_id: 1 }
]
}
Any tips/advice for this problem would be greatly appreciated.
There are several little things that prevent your fiddle to work:
the DS.hasMany function asks for a String as argument. Don't forget the quotes: DS.hasMany('Node')
in the fixture definition, hasMany relationships should not be postfixed by _ids or anything. Just use the plain name. For instance: { id: 42, children: [2,3], parent_id: 17 }
the length property of DS.ManyArray should be accessed using the get function: root.get('children.length')
by default, the fixture adapter simulates an ajax call. The find query will populate the record after waiting for 50ms. In your fiddle, the root.get('children.length') call comes too early. You can configure the fixture adapter so that it makes synchronous call:
App.store = DS.Store.create({
revision: 4,
adapter: DS.FixtureAdapter.create({
simulateRemoteResponse: false
})
});
Or you can load data to the store without any adapter:
App.store.loadMany(App.Node, [
    { id: 1, children: [2, 3] },
    { id: 2, children: [], parent_id: 1 },
    { id: 3, children: [], parent_id: 1 }
]);
and last one: it seems like the Ember app should be declared in the global scope (no var), and Ember-data models should be declared in the app scope (replacing var Node = ... by App.Node = ...)
Full example:
App = Ember.Application.create();
App.store = DS.Store.create({
    revision: 4
});
App.Node = DS.Model.extend({
children: DS.hasMany('App.Node'),
parent:   DS.belongsTo('App.Node')
});
App.store.loadMany(App.Node, [
{ id: 1, children: [2, 3] },
{ id: 2, children: [], parent_id: 1 },
{ id: 3, children: [], parent_id: 1 }
]);
var root = App.store.find(App.Node, 1);
alert(root.get('children'));
alert(root.get('children.length'));
This didn't work for me until I set up the inverse:
App.Node = DS.Model.extend({
children: DS.hasMany('App.Node', {inverse: 'parent'}),
parent: DS.belongsTo('App.Node', {inverse: 'children'}) });
Not sure but as per example given in ember guide
App.Post = DS.Model.extend({
comments: DS.hasMany('App.Comment')
});
The JSON should encode the relationship as an array of IDs:
{
"post": {
"comment_ids": [1, 2, 3]
}
}