Ember.js - how to fetch additional record details? - ember.js

Struggling with populating Ember with data.
I'm using Rails as the backend, and when I hit /contacts.json (ContactsRoute), it returns a list of id, first, last -- works as expected.
However, when visiting a detail view (ContactRoute), I would like to hit /contacts/1.json and fetch details like email address, anniversaries, etc. But since I have a dynamic segment the model hook is skipped and none of the associations are available.
Question: what is the best approach for fetching additional data in a list/detail scenario?
Models:
App.Contact = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
company: DS.attr('string'),
emails: DS.hasMany('App.Email'),
});
App.Email = DS.Model.extend({
contact: DS.belongsTo('App.Contact'),
emailAddress: DS.attr('string'),
});
Route:
App.Router.map(function() {
this.resource('contacts');
this.resource('contact', {path: 'contacts/:contact_id'});
});
App.ContactsRoute = Ember.Route.extend({
init: function() {},
model: function() {
return App.Contact.find();
}
});
App.ContactRoute = Ember.Route.extend({
model: function(params) {
return App.Contact.find(params.contact_id);
}
});
Thanks in advance.

I just posted an answer to a similar problem here: https://stackoverflow.com/a/18553153/1254484
Basically, in your App.ContactRoute, override the setupController method:
setupController: function(controller, model) {
controller.set("model", model);
model.reload();
return;
}
I'm using this with the latest ember-data (commit ef11bff from 2013-08-26).

Related

Ember Data - how to findBy with an associated hasMany

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.

Ember - How do you preselect items in multiselect when using hasMany attribute?

I am currently having issues preselecting items in the multiselect view. I'm using ember 1.3 with ember-data 1.0.0 beta 6 and ember-data-django-rest-adapter.
App.Article = DS.Model.extend({
title: attr(),
description: attr(),
authors: hasMany('author')
});
App.ArticleRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('article', params.article_id);
},
setupController: function(controller, model) {
controller.set('content', model);
}
});
App.ArticleController = Ember.ObjectController.extend({
needs: ['authors'],
allAuthors: function() {
return this.store.find('author');
}.property()
});
Template:
{{input authors as='select'
multiple='true'
collection='allAuthors'
selection='authors'
optionValuePath='content.id'
optionLabelPath='content.name'}}
I'm not sure why this is not working because when I output allAuthors and authors using #each in the template, I'm getting the data that I should.
Is there something I'm missing to do?
Thanks in advance for the help.
I usually prepopulate this kind of data in the route using a promise:
App.ArticleRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('article', params.article_id);
},
setupController: function(controller, model) {
this.store.find('author').then(function(authors) {
controller.set('allAuthors', authors);
// or maybe controller.get('allAuthors').addObjects(authors);
});
controller.set('content', model);
}
});
App.ArticleController = Ember.ObjectController.extend({
needs: ['authors'],
allAuthors: []
});
Not sure if this is the best way to do this, but it's what worked for me.

Why is "Assertion failed: You can only add a 'story' record to this relationship" is being thrown?

I don't think I understand how to set a belongsTo relationship. I'm using
ember-1.1.2, and ember data beta3. Any help appreciated.
The relationship definitions:
App.Story = DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
setting: DS.attr('string'),
status: DS.attr('string'),
chapters: DS.hasMany('chapter',{async: true}),
cast: DS.hasMany('actor', {async: true})
});
App.Chapter = DS.Model.extend({
name: DS.attr('string'),
number: DS.attr('number'),
description: DS.attr('string'),
story: DS.belongsTo('story'),
scenes: DS.hasMany('scene',{async: true})
});
The routes:
this.resource('story', {path: '/story'}, function() {
this.route('edit', {path: '/:story_id'})
this.route('new')
this.resource('chapter', {path:"/:story_id/chapter"}, function() {
this.route('edit', {path: '/:chapter_id/edit'})
this.route('new')
this.resource('scene', {path:":chapter_id/scene"}, function() {
this.route('edit', {path: '/:scene_id/edit'})
this.route('new')
})
})
})
Where the error occurs:
App.ChapterNewRoute = Ember.Route.extend({
setupController: function( controller, model) {
this.controllerFor('chapter.edit').setProperties({isNew:true, content:model})
},
model: function(params) {
var chapter = this.store.createRecord('chapter')
this.store.find('story', params.story_id).then(function( story) {
chapter.set('story', story) //ERROR HAPPENS HERE
story.get('chapters').push(chapter)
})
return chapter
},
renderTemplate: function() {
this.render('chapter.edit')
}
})
story_id doesn't exist in that route's model hook, it only lives on the ChapterRoute (aka story_id is undefined and you are probably not getting a story). You can use modelFor to get the model from the chapter's route and get the from that model if it exists.
Here's a jsbin showing it working
http://emberjs.jsbin.com/OxIDiVU/1/edit

Load additional data into an EmberData model that is still in the store

I have a list of products that will be loaded under the /products route, from there you can navigate to a single product under the /products/:product_id. This is my models and the route:
var Product = DS.Model.extend({
page_title: DS.attr('string'),
image: DS.attr('string')
});
var ProductComment = DS.Model.extend({
contents: DS.attr('string')
});
var ProductRoute = Ember.Route.extend({
model: function(params) {
return App.Product.find(params.product_id)
},
setupController: function(controller, model) {
controller.set('content', model);
}
});
On the product page I want to load the products and additionally the comments for a product. As I use an external Api I cant load the id of the comments into the product model. So now I want to load the comments in to the ProductsController. I tried like described in this SO but it doesn't work. I'm using EmberDatas RESTAdapter.
I came up with solution. In the modelAfter hook of the products route, check if the comments are loaded into the model using this.get('product_comments').content.length. If not, load the data using App.ProductComment.find({product_id: this.id}) and store them into the model.
App.ProductRoute = Ember.Route.extend({
afterModel: function(model) {
model.ensureComments();
}
});
Product = DS.Model.extend({
page_title: DS.attr('string'),
image: DS.attr('string'),
product_comments: DS.hasMany('App.ProductComment'),
ensureComments: function() {
var productcomments = this.get('product_comments');
if (!productcomments.content.length) {
App.ProductComment.find({product_id: this.id}).then(function(result) {
productcomments.pushObjects(result)
});
}
}
});

Ember.js - Cannot render nested models

Have an upcoming weekend project and using it to evaluate Ember.js and I cannot figure out why I cannot display nested objects in my template. This does not work:
{{#each emails}}
{{email_address}}
{{/each}}
When I try just {{emails}} I get a hint that something is right:
Models:
App.Contact = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
company: DS.attr('string'),
emails: DS.hasMany('App.Email')
});
App.Email = DS.Model.extend({
contact: DS.belongsTo('App.Contact'),
emailAddress: DS.attr('string'),
});
Route:
App.Router.map(function() {
this.resource('contacts', function() {
this.resource('contact', {path: ':contact_id'});
});
});
App.ContactsRoute = Ember.Route.extend({
init: function() {},
model: function() {
return App.Contact.find();
}
});
App.ContactRoute = Ember.Route.extend({
model: function(params) {
return App.Contact.find(params.contact_id);
}
});
I have no idea what to try next. I'm using active_model_serializer in Rails. I've tried embedding, side-loading to no avail. I'm sure it's something simple that I'm missing.Thanks in advance!
When using the each helper it's recomendable to be more specific about the items you are looping over to avoid such problems.
Try the following:
{{#each email in model.emails}}
{{email.emailAddress}}
{{/each}}
This should also work:
{{#each emails}}
{{this.emailAddress}}
{{/each}}
And also, your model property is called emailAddress and not email_address.
Hope it helps.