I want to use my File Model with two other models.
App.File = DS.Model.extend({
filename: DS.attr('string'),
//related:DS.belongsTo('App.Task' and 'App.Comment'),
});
App.Task = DS.Model.extend({
title: DS.attr('string'),
files:DS.hasMany('App.Files'),
});
App.Comment = DS.Model.extend({
comment:DS.attr('string'),
files:DS.hasMany('App.Files'),
});
The database structure has a related_id and related_type column. How can I set this up to work with ember?
Related
I have a model called group having multiple attributes, I also want to save my id information to some other attribute because of some reasons to pass the model info to somewhere else.
import DS from 'ember-data';
var group = DS.Model.extend({
groupId : DS.attr('string'),//want to duplicate id here
groupName: DS.attr('string'),
groupRegion: DS.attr('string'),
groupCountry: DS.attr('string'),
groupSegment: DS.attr('string'),
groupSubSegment: DS.attr('string'),
isSelected: DS.attr('boolean'),
isExpand: DS.attr('boolean')
});
export default group;
You can make use of the ready hook.
var group = DS.Model.extend({
groupId : DS.attr(),//want to duplicate id here
groupName: DS.attr('string'),
groupRegion: DS.attr('string'),
groupCountry: DS.attr('string'),
groupSegment: DS.attr('string'),
groupSubSegment: DS.attr('string'),
isSelected: DS.attr('boolean'),
isExpand: DS.attr('boolean'),
ready(){
this.set('groupId',this.get('id'));
}
});
So lets say a recipe has several ingredients of differing amounts.
Recipe Model
var Recipe = DS.Model.extend({
name: DS.attr('string'),
ingredients: DS.hasMany('ingredient')
});
Ingredient Model
var Ingredient = DS.Model.extend({
name: DS.attr('string'),
recipes: DS.hasMany('recipe'),
// amount?
});
So the amount of each ingredient would depend on the recipe. However on its own the ingredient will not have an amount.
How would you go about modeling that data? Right now I am using the FixtureAdapter until I finish building the interface.
Using Ember 1.5.1 and Ember-Data 1.0.0-beta.7+canary.b45e23ba.
To answer your first question:
Define the model like so
App.Comment = DS.Model.extend({
message: DS.belongsTo('message', {
polymorphic: true
})
});
And the property needs an additional property propertyType, defining the relationship type
{
"message": 12,
"messageType": "post"
}
https://github.com/emberjs/data/blob/master/TRANSITION.md#polymorphic-relationships
Now to your second question, not sure if polymorphism would be necessary. I might just include a joining record
var RecipeIngredient = DS.Model.extend({
amount: DS.attr(),
ingredient: DS.belongsTo('ingredient')
});
var Recipe = DS.Model.extend({
name: DS.attr('string'),
ingredients: DS.hasMany('recipeIngredient')
});
I am in the process of porting ember-data .13 to the latest ember-data and can't get polymorphic hasMany-relationships to work correctly.
I have a model Person. A person has many posts. The posts are polymorphic, so there are photoPosts, textPosts, ...
The posts should be loaded in an async request, but the only way to get polymorphism to work is by sideloading the tasks. (http://jsfiddle.net/koenig/3qwEP/7/)
Here are my models
App.Post = DS.Model.extend({
title: DS.attr('string'),
person: DS.belongsTo('person')
});
App.PhotoPost = App.Post.extend({
photoUrl: DS.attr('string'),
postType: 'photo'
});
App.TextPost = App.Post.extend({
body: DS.attr('string'),
postType: 'text'
});
App.Person = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
posts: DS.hasMany('post', {async: true, polymorphic: true}),
fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
}.property('firstName', 'lastName')
});
This is my payload for persons:
person: {
id: 1,
first_name: 'luke',
last_name: 'skywalker',
links: { posts: '/people/1/posts' }
}
This for posts:
posts: [
{id: 1, title: 'A Photo', type: "photoPost" },
{id: 2, title: 'Some Text', type: "textPost" }
]
I also tried different payloads and variations, played around with various serializer hooks and looked for the answer in ember-data code. No luck yet.
Am I missing something or are async polymorphic relationships not possible in ember data?
Here is a jsfiddle with the basic setup: http://jsfiddle.net/koenig/3qwEP/
Thanks in advance.
I'm trying to display a model, with a hasMany relation and each of those relations has a belongsTo relation.
For some reason, Ember doesnt want to display the belongsTo.
Here are my models:
App.City = DS.Model.extend({
city: DS.attr('string')
});
App.Child = DS.Model.extend({
name: DS.attr('string'),
city: DS.belongsTo('city', {async: true})
});
App.Activity = DS.Model.extend({
children: DS.hasMany('child',{async:true}),
name: DS.attr('string')
});
My template looks like this:
Activity name: {{name}}<br />
{{#each child in children}}
Child name: {{child.name}}<br />
Child city name: {{child.city.name}}
{{/each}}
{{child.city.name}} is empty.
I've created a JSFiddle to illustrate the problem here: http://jsfiddle.net/N2xdx/
In your City fixtures you have:
App.City.FIXTURES = [
{
id: 1,
name: 'Aarhus'
}
];
But your App.City doesn't have a name: DS.attr('string') mapping. Update your model to the following, and all will work:
App.City = DS.Model.extend({
name: DS.attr('string'),
city: DS.attr('string')
});
This is a fiddle with this working http://jsfiddle.net/marciojunior/vDaxt/
Let's say my rails models look like this:
class SalesRelationship < ActiveRecord
end
Which is inherited by crossSell like this:
class crossSell < SalesRelationship
end
How do I show this inheritance relationship in ember-data. What is the best practise for this:
App.salesRelationship = DS.Model.extend({
name: DS.attr('string')
});
Can I create a subclass called 'crossSell', like this
crossSell = App.salesRelationship({
productName: DS.attr('string')
});
or like this
App.salesRelationship.crossSell = DS.Model.extend({
productName: DS.attr('string')
});
Pretty close, you can just extend SalesRelationship.
App.CrossSell = App.SalesRelationship.extend({
productName: DS.attr('string')
})
In Ember 2.7 it can be done like this. Assume you have a Person class and wish to inherit from it to make an Employee for a status field (like hired, retired, fired on-leave etc)
app/models/person.js
import DS from 'ember-data';
export default DS.Model.extend({
firstName: DS.attr(),
lastName: DS.attr(),
fullName: Ember.computed('firstName', 'lastName', function() {
return `${this.get('lastName')}, ${this.get('firstName')}`;
});
app/models/employee.js
import DS from 'ember-data';
import Person from './person';
export default Person.extend({
status: DS.attr(),
statusCode: DS.attr(),
});