The model I have:
Post = DS.Model.extend({
author: DS.belongsTo('user',{ async: true })
});
but the url for the author is /post/{id}/author and it is passed via a key-value in the links-hash of the payload:
{
post: {
id: 1,
links: {
author: 'author'
}
}
}
The data is retreived by post.get('author') which calls the url /posts/1/author.
But now I want to reload the author data. When I try:
post.get('author').then(function(author) {
author.reload();
});
the author is reloaded by calling /users/{author_id} instead of /posts/1/author. How can I fix this issue?
Related
This is my Ember inspector info tab:
I am using ember-cli-mirage and I'm facing issue here.
I am not able to get belongsTo relationship of a model. When I do car.get('user'), I get a promise in result which gets fullfilled but the value is always null.
My user model
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('String'),
car: DS.belongsTo('car',{async: true})
});
My car model
import DS from 'ember-data';
export default DS.Model.extend({
color: DS.attr('String'),
user: DS.belongsTo('user',{async: true})
});
my mirage/config
this.get('/cars',function(db,request) {
var qp = request.queryParams.searchString.toLowerCase();
return {
cars: db.cars.where({'color':qp}),
users: db.users
};
});
I get the list of cars with search color but I don't get the user.
it returns a promise which when fullfilled gives null.
Since I am using ember-data 1.13.8
I tried using this Mirage working with json but then I get error
datum model is not defined error so i guess new json api is not my issue for some reason my ember data uses old json format and original config works.
this.get('/cars', function(db, request) {
return {
data: db.cars.map(attrs => (
{type: 'cars', id: attrs.id, attributes: attrs }
))
};
})
My cars route look like this
model: function(params) {
self = this;
if(!params){
return [];
}
return this.store.findQuery('car',params);
},
I tried
return this.store.findQuery('car',params).then(function(item){
console.log(item.get('user'));
});
but i still got console.log //undefined
I can see user_id in my json returned.
SOLUTION:
found the issue i was saving relationship as user_id = user.id. It should be user=user.id
SOLUTION: found the issue i was saving relationship as user_id = user.id. It should be user=user.id
I'm trying query my post model for published posts that have a specific tag. My models look like this:
var Post = DS.Model.extend({
tags: DS.hasMany('tag', { async: true }),
title: DS.attr('string'),
published: DS.attr('boolean'),
});
var Tag = DS.Model.extend({
posts: DS.hasMany('post', { async: true }),
name: DS.attr('string')
});
I've been able to get published posts in my query using the find() method:
this.get('store').find('post', {published: true});
But I don't understand how to query for properties that are part of a related model. I'd appreciate any guidance, thanks!
Edit
I came up with this solution using filter() which seems to work well.
var self = this;
var store = this.get('store');
store.find('post', {published: true}).then(function() {
self.set('content', store.filter('post', function(post) {
return post.get('tag.name') === 'Photography';
}));
});
I am very new at Ember/ED/EmberFire so apologies if this is a trivial question. I am able to save records to Firebase but I am unable to specify relationships to those records in my controller. I am using
DEBUG: Ember : 1.10.0
DEBUG: Ember Data : 1.0.0-beta.12
DEBUG: Firebase : 2.2.3
DEBUG: EmberFire : 1.4.3
DEBUG: jQuery : 1.11.2
I have a model as such:
var Patient = DS.Model.extend({
lastName: DS.attr('string'),
firstName: DS.attr('string'),
encounters: DS.hasMany('encounter', {async: true})
});
var Encounter = DS.Model.extend({
dateOfEncounter: DS.attr('string'),
patient: DS.belongsTo('patient', {async: true})
});
I am simply trying to specify the patient that my newly created encounter object is associated with. This is my controller:
actions: {
registerEncounter: function() {
var newEncounter = this.store.createRecord('encounter', {
dateOfEncounter: this.get('dateOfEncounter'),
patient: this.store.find('patient', '-Jl8u8Tph_w4PMAXb9H_')
});
newEncounter.save();
this.setProperties({
dateOfEncounter: ''
});
}
}
I can successfully create the encounter record in Firebase, but there is no associated patient property. All I get is
https://github.com/firebase/emberfire/issues/232
this.store.find('patient', '-Jl8u8Tph_w4PMAXb9H_') returns a promise.
Try this:
this.store.find('patient', '-Jl8u8Tph_w4PMAXb9H_').then(function(pat) {
var newEncounter = this.store.createRecord('encounter', {
dateOfEncounter: this.get('dateOfEncounter'),
patient: pat
});
newEncounter.save();
});
I have two objects User and Post. A user has many posts and a post belongs to a user.
How do I, within the user controller, use findBy or another method to get to a featured post with the posts array??
Here is how I implemented the UserController; however, the featuredPost computed property is coming back as undefined. Is this best approach? If so, what am I missing?
App.User = DS.Model.extend({
name: DS.attr('string'),
email: DS.attr('string'),
client: DS.belongsTo('App.Client', { async: true }),
posts: DS.hasMany('App.Post', { async: true })
});
App.Post = DS.Model.extend({
client: DS.belongsTo('App.Client', { async: true }),
user: DS.belongsTo('App.User', { async: true }),
title: DS.attr('string'),
body: DS.attr('string'),
isFeatured: DS.attr('boolean')
});
App.UserController = Ember.ObjectController.extend({
needs: ['post'],
posts: (function() {
return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
content: this.get('content.posts')
});
}).property('content.posts'),
featuredPost: (function() {
return this.get('content.posts').findBy('isFeatured', true)
}).property('content.featuredPost'),
});
Take a look at this: http://emberjs.com/api/#method_computed_filterBy
App.UserController = Ember.ObjectController.extend({
featuredPost: Ember.computed.filterBy('posts', 'isFeatured', true)
});
Also, in
featuredPost: (function() {
return this.get('content.posts').findBy('isFeatured', true);
}).property('content.featuredPost')//should be observing 'posts'
You are basically observing content.featuredPost but from what youve mentioned that property doesnt exist, the property you should be observing is 'posts'. This is a mistake that i made when i was learning ember too, so felt like pointing out. Also using content is optional, you can directly observe the model associated with controller.
Also From doc, findBy seems to return just first item that matches the passed value, not all of them. So to get first match it should be
App.UserController = Ember.ObjectController.extend({
featuredPost: function() {
return this.get('posts').findBy('isFeatured', true);
}.property('posts')//assuming user model hasMany relation to posts
});
Also I would go with the latest version of ember data and make following changes:
App.User = DS.Model.extend({
name: DS.attr('string'),
email: DS.attr('string'),
client: DS.belongsTo('client', { async: true }),
posts: DS.hasMany('post', { async: true })
});
App.Post = DS.Model.extend({
client: DS.belongsTo('client', { async: true }),
user: DS.belongsTo('user', { async: true }),
title: DS.attr('string'),
body: DS.attr('string'),
isFeatured: DS.attr('boolean')
});
This would be good read : https://github.com/emberjs/data/blob/master/TRANSITION.md
And here is a bare minimum working example : http://jsbin.com/disimilu/5/edit
Hope this helps.
App.User = DS.Model.extend({
posts: DS.hasMany('post', {async: true})
});
App.Post = DS.Model.extend({
body: DS.attr(),
user: DS.belongsTo('user')
});
App.ProfileRoute = Ember.Route.extend({
model: function(params) {
return this.get('store').find('user', params.user_id)
}
});
and in template
{{#each post in model.posts}}
{{post.body}}
{{/each}}
json for user. I don't want embed posts in user json
{user: { posts: [1, 2, 3] }}
This don't render anything. It receives posts json from server after this error occur
Assertion failed: You looked up the 'posts' relationship on '' but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (DS.attr({ async: true }))
In chrome inspector I see all data loaded properly.
How can I solve this? Should I preload all models I want to use in templates?
The model function in your route is missing the return, so the error is being thrown when you try to access model.posts because there is no model.
App.ProfileRoute = Ember.Route.extend({
model: function(params) {
return this.get('store').find('user', params.user_id);
}
});
Have you tried to just write {{#each posts}}?
Worked for my project.
Then write {{body}} within the each block.
Let me know, thanks!