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
Related
I have a template like so:
{{#each rate in package.ratesSorted}}
{{rate.year}}
{{/each}}
I want the rates to be sorted by ascending year, so I have:
var Package = DS.Model.extend({
rates: DS.hasMany('rate'),
ratesSorted: Ember.computed.sort('rates', (a, b) => {
return a.get('year') > b.get('year');
})
});
Package.reopenClass({
FIXTURES: [
{id: 1, rates: [1, 3, 4]}
]
});
And my rate Model looks like this:
var Rate = DS.Model.extend({
year: DS.attr('number'),
});
Rate.reopenClass({
FIXTURES: [
{id: 4, year: 4},
{id: 1, year: 1},
{id: 3, year: 3}
]
});
But it then ends up being sorted 3,4,1. Why?
It's because Ember.computed.sorted expects -1 and 1 rather than true or false.
var Package = DS.Model.extend({
ratesSorted: Ember.computed.sort('rates', (a, b) => a.get('year') < b.get('year') ? -1 : 1)
});
Works. Using sorting definitions, a better version is:
var Package = DS.Model.extend({
rateSorting: ['year'],
ratesSorted: Ember.computed.sort('rates', 'rateSorting')
});
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]
}
]
here is a working jsbin: http://emberjs.jsbin.com/EnOqUxe/71/edit
What i´d like to have is there the company doesn´t need any reference to the person.
non working code
App.Company.FIXTURES = [
{ id: 1, name: 'Microsoft'},
{ id: 2, name: 'Apple'}
];
App.Person.FIXTURES = [
{ id: 1, name: 'Steve Jobs', company:2},
{ id: 2, name: 'Bill Gates', company:1},
{ id: 3, name: 'Steve Ballmer', company:1}
];
How can i achieve this?
thank you
You're practically there. You just need to fix up the models a bit:
App.Company = DS.Model.extend({
name: DS.attr('string')
});
App.Person = DS.Model.extend({
name: DS.attr('string'),
company: DS.belongsTo('company', {async:true})
});
And change your model hook, since now you link to companies through people, not people through companies.
App.IndexRoute = Ember.Route.extend({
model: function() {
return this.store.find('person');
}
});
http://emberjs.jsbin.com/EnOqUxe/72/edit
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?
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' })