Hi,
In my ember app (I use ember-cli to generate stuff), I have several problems and questions. I am new to ember and I don’t have a frontend background.
//router.js
Router.map(function() {
this.route('categories');
this.route('category', { path: 'categories/:category_id' }, function() { });
});
//templates/application.hbs
{{outlet "header"}}
{{outlet "content"}}
{{outlet "footer"}}
//routes/categories.js
export default Ember.Route.extend({
model: function() {
return Ember.RSVP.hash({
categories: this.store.filter('category')
});
},
setupController: function(controller, model) {
controller.set('categories', model.categories);
},
renderTemplate: function() {
this.render('header', {
into: 'application',
outlet: 'header',
controller: 'header'
});
this.render('categories', {
into: 'application',
outlet: 'content',
controller: 'categories'
});
this.render('footer', {
into: 'application',
outlet: 'footer',
controller: 'footer'
});
}
});
//routes/category.js
export default Ember.Route.extend({
model: function(params) {
return Ember.RSVP.hash({
categories: this.store.filter('category'),
category: this.store.fetch('category', params.category_id)
});
},
setupController: function(controller, model) {
controller.set('categories', model.categories);
controller.set('category', model.category);
},
renderTemplate: function(model) {
this.render('header', {
into: 'application',
outlet: 'header',
controller: 'header'
});
this.render('category', {
into: 'application',
outlet: 'content',
controller: 'category'
});
this.render('footer', {
into: 'application',
outlet: 'footer',
controller: 'footer'
});
}
});
//templates/categories.hbs
{{categories-category categories=categories}}
//templates/components/categories-category.hbs
{{#each category in categories}}
{{#link-to 'category' category}}
<div>{{category.name}}</div>
<img {{bind-attr src="category.getImage" alt="category.name"}}>
{{/link-to}}
{{/each}}
//templates/category.hbs
{{#if category.hasChildren}}
<h1>{{category.name}}</h1>
{{category-category categories=category.children_ids}}
{{else}}
NOT OK
{{/if}}
I also have a model category.js that uses REST to get the categories from an external API. When I access dev:4200/categories I can see my categories but if I click on their images the url changes to dev:4200/categories/10 but the text loaded is “NOT OK”, and it doesn’t do any ajax request. If I refresh this page, I have 2 ajax requests: api/categories and api/categories/10, also the category name and children are properly loaded and displayed.
- What am I doing wrong, why I have access to category only after I refresh the page? (maybe I misunderstood something with Ember)
- Also, I would like to have only one ajax request (the one for categories) and then filter by category_id to get the category every time, is this possible?
- I want to move the code to get the categories inside routes/application.js Pushing Records into the Store - Ember Data - Ember Guides “its model hook gets called once when the app starts up” I couldn’t find a way to make the application.categories available in my other routes, is this possible?
I want to state again that I’m using ember-cli with:
DEBUG: Ember : 1.8.1
DEBUG: Ember Data : 1.0.0-beta.12
DEBUG: Handlebars : 1.3.0
DEBUG: jQuery : 1.11.2
Thank you.
LE: On my first question, if if visit categories, then categories/10 and then I hit back and forward, the page will make the ajax request and have the category information (display right template…)