I have a database table with emails and one with persons. I need to make a relationship from the parent to the child. The emails don´t have any reference to the person except the from-email .How to setup the models with the following json:
App.Person.FIXTURES = [
{name:"Peter",id:"1",email:example#mail.com},
{name:"Dan",id:"2",x#y.com}
];
App.Mails.FIXTURES = [
{id: 1, subject "text", email: example#mail.com },
{id: 2, subject "text2", email: x#y.com },
{id: 3, subject "text3", email: x#y.com },
];
i´ve tried:
App.Mail = DS.Model.extend({
email: DS.belongsTo('person'),
subject: DS.attr(),
name: DS.attr(),
});
App.Person = DS.Model.extend({
mails: DS.hasMany('mail',{inverse: 'email'}),
email: DS.attr(),
});
JSBIN: http://jsbin.com/yeyemi/3/edit
How can i get this working without the Mails have to reverence anything exept the E-Mail to the Person Model?
Related
is there any way to do some filtering based on the related ID? imagine I have 2 models one is a song model and the other is an album model so obviously the song has album with an id, the json will return the model with
{album: 1}
so if I wanna filter it seems that I can't compare the album id with something like
song.get('album.id') === 1
is there any way to do it so simply?
Thanks
Edit: models
App.Album = DS.Model.extend({
irrelevantstuff: DS.attr(),
songs: DS.hasMany('Song')
})
App.Song = DS.Model.extend({
irrelevantstuff: DS.attr(),
album: DS.belongsTo('Album')
})
I tried both with {async:true} and without, still same problem.
however I noticed that if I run it the first time it gives me undefined to all of the album.id but if I run it the second time I get the ids, now I have everything on the store, so it's not doing an AJAX request either the first or second time.
{"songs": [{"irrelevantstuff":"foo", "album": 1}, {"irrelevantstuff":"bar", "album": 1}]}
{"albums": [{"irrelevantstuff":"album", "songs": [1,2]}]
Relationships should be camelCase
App.Album = DS.Model.extend({
irrelevantstuff: DS.attr(),
songs: DS.hasMany('song', {async: true})
})
App.Song = DS.Model.extend({
irrelevantstuff: DS.attr(),
album: DS.belongsTo('album', {async: true})
})
Since your data for the relationship isn't coming down in the request for the song/album it would be considered async. As such if you want to access it you'll need to use it asynchronous methods, promises.
var songPromise = this.store.find('song', 1);
songPromise.then(function(song){
var albumPromise = song.get('album');
albumPromise.then(function(album){
// the album is available for usage....
if(album.get('id') == 1){
alert('woo hoo');
}
});
});
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 am trying to set model of a route(not nested) but with relationship belong to with setupController
Here is my JSBIN
i.e company hasMany Contacts
from companies list page I have directly linked to contacts page (want flow that ways)
I want to use the company model to get its contacts list but I am getting error
Error while loading route: TypeError: Cannot call method 'resolve' of undefined
Heres my Router
App.Router.map(function() {
this.resource('companies',{path:'/'});
this.resource('company',{path:'/company/:company_id'});
this.resource('contacts',{path:'/company/:id/contacts'});
});
This is what I am doing
App.ContactsRoute = Em.Route.extend({
setupController: function(controller, model) {
console.log(model.get('contacts')); //This line gives error
//controller.set('model',model.get('contacts'));
}
});
The MODEL
App.Company = DS.Model.extend({
name: DS.attr('string'),
contacts: DS.hasMany('Contact')
});
App.Company.FIXTURES = [{
id: 1,
name: 'company 1',
contacts: ['1']
}, {
id: 2,
name: 'company 2',
contacts: ['1', '2']
}];
App.Contact = DS.Model.extend({
name: DS.attr('string'),
company: DS.belongsTo('Company')
});
App.Contact.FIXTURES = [{
id: 1,
name: 'employee 1',
company: 1
}, {
id: 2,
name: 'employee 2',
company: 2
}];
Updated Fiddle
I updated the fiddle for you and changed a few things. To me, it seemed more logical to add contacts as a route of the resource company instead of a separate resource.
I also made the company model async, so it waits for all models to be loaded before actually rendering everything
Take a look and feel free to ask questions if you don't fully understand the solution.
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/
JSFiddle - http://jsfiddle.net/9gA4y/1/
I have following model:
contact => (many2many) => tags
contact => (one2many) => address
Ember Data Model:
App.Contact = DS.Model.extend({
name: attr('string'),
tags: hasMany('App.Tag'),
addresses: hasMany('App.Address')
});
App.Address = DS.Model.extend({
street: attr('string'),
country: attr('string'),
contacts: belongsTo('App.Contact')
})
App.Tag = DS.Model.extend({
name: attr('string'),
contacts: hasMany('App.Contact')
});
I figured out adding New contact record
How do I associate existing, Address to Newly created contact. (one 2 Many)
How do I associate existing, Tags to Newly created contact. (Many 2 Many)
How do I delete associations in a existing contact.
hasMany relationships can be manipulated through addObject, addObjects or removeObject.
contact.get('addresses').pushObject(address);
contact.get('addresses').removeObject(address);
You could also set contact on the address
address.set('contact', contact);
address.set('contact', null);
Also, you note that you should use the singular form for a belongsTo association (contact not contacts):
App.Address = DS.Model.extend({
street: attr('string'),
country: attr('string'),
contact: belongsTo('App.Contact')
});
Try:
var contact = App.Contact.find(1),
address = App.Address.find(1),
tag = App.Tag.find(1);
contact.get('addresses').addObject(address);
contact.get('tags').addObject(tag);
this.get('store').commit(); //saves address and tag to contact
contact.get('tags').removeObject(tag);
this.get('store').commit(); //removes tag from contact