The following structure of routes works fine for me.
But whenever I moved faculty.js inside the faculty folder, my template stops recognizing the data. The static view elements still show up correctly, but the data model is logged as null. How can I fix it?
faculty.js
import Ember from 'ember';
export default Ember.Route.extend({
model(){
return {name:"Janusz", lastname:"Chudzynski", department:"Test"};
}
});
faculty.hbs
{{outlet}}
<h2>Faculty</h2>
{{log model}}
{{faculty-single facultyModel=model}}
Router
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: config.locationType,
rootURL: config.rootURL
});
Router.map(function() {
this.route('colleges');
this.route('departments');
this.route('faculty',{path:'/faculty'});
//this.route('faculty');
});
export default Router;
Console log
null
The hierarchy of your folders should closely mirror the hierarchy of your router. In your case you have the following:
Router.map(function() {
this.route('colleges');
this.route('departments');
this.route('faculty');
});
This means that all of them are directly under the application route, being the correct file structure:
app
routes
colleges.js
departments.js
faculty.js
For /app/routes/faculty/faculty.js to work, you would need to have a faculty route nested inside a faculty route:
Router.map(function() {
this.route('colleges');
this.route('departments');
this.route('faculty', function() {
this.route('faculty');
});
});
Since names concatenate, the parent route's full name is faculty, and the nested route's full name is faculty.faculty.
Related
I am a beginner to Ember.js so I took the Codeschool course 'Try Ember'. So following this course I actually get an error.
My router.js file looks like this:
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: config.locationType,
rootURL: config.rootURL
});
Router.map(function() {
this.route('orders', function(){
this.route('order', {path: '/:order_id'});
});
});
export default Router;
Now as far as I understand from the tutorial I have two routes orders.js and order.js with templates templates/orders.hbs and templates/orders/order.hbs respectively.
orders.js file:
import Ember from 'ember';
export default Ember.Route.extend({
model(){
return [
{ id: '1', name: 'Vlatko'},
{ id: '2', name: 'Mila'}
];
}
});
order.js file:
import Ember from 'ember';
export default Ember.Route.extend({
model(params){
return [
{ id: '1', name: 'Vlatko'},
{ id: '2', name: 'Mila'}
].findBy('id', params.order_id);
}
});
templates/orders.hbs file:
<h2>Hello from orders</h2>
{{#each model as |order|}}
<p>
{{#link-to 'orders.order' order}}
Order {{order.id}}
{{/link-to}}
</p>
{{/each}}
{{outlet}}
templates/orders/order.hbs file:
<p>Order {{model.id}} for {{model.name}}</p>
So everything is pretty simple and works well, but when I try to do a full page reload(enter directly on the page) /orders/1 it raises two errors
Error while processing route: orders.order No model was found for 'order' Error: No model was found for 'order'
and
Error: No model was found for 'order'
Now, I've searched a lot on the web and I can't find the same error.
An additional hint: This only happens when I use nested routes. If for instance I have something like this in my router.js:
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: config.locationType,
rootURL: config.rootURL
});
Router.map(function() {
this.route('orders');
this.route('order', {path: '/orders/:order_id'});
});
export default Router;
I get no error.
Your order.js file should be orders/order.js. Your problem is that ember doesn't find your route so you get the default route. The default model hook with a dynamic segment order_id will basically do store.findRecord('order', theId), and so you get the error that the model order is not defined because you don't use ember-data.
Pretty new to Ember so maybe someone can help me out. I keep running across this error and have no idea how to solve it.
Ember : 2.5.1
Ember Data : 2.5.3
Below is my router.js.
//app/router.js
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.route('organization', {path: '/organization/:id'}, function(){
this.route('about', { path: '/about' });
this.route('admin', { path: '/admin' }, function(){
this.route('team', { path: '/team/:team_id' });
});
});
});
The organization/:id/about and organization/:id/admin routes work fine. But when I try to load the organization/:id/admin/team/:team_id route, the error is thrown. Below is the routes/organization/admin/team.js file:
//app/routes/organization/admin/team.js
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
let organization = this.modelFor('organization');
return organization.get('team');
}
});
Not really sure what other information I should post, so please ask for any additional information you may think is necessary to help debug. My guess is it's something pretty simple and I'm completely oblivious to it.
EDIT
I've added a couple more files to help diagnose the problem:
//app/routes/organization.js
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
return this.store.findRecord('organization', params.organization_id)
}
});
//app/routes/organization/admin.js
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
changeValue(){
this.currentModel.save();
}
}
});
Where currentModel is the model for the organization route. I've removed the organization.admin.team model hook for now and am just testing a
{{#link-to 'organization.admin.team' model.team.id}} Team {{/link-to}}
in a component rendered in the organization.admin template where I pass model=model. But now I get the same error (Assertion Failed: You need to pass a model name to the store's modelFor method) in the Javascript console when rendering the organization.admin template.
If you pass Object to {{#link-to}} helper. It skips the model hook. So you could basically send {{#link-to 'team' organization.team}}Without having to write "model" hook.
"It makes sense and it might save a request to the server but it is, admittedly, not intuitive. An ingenious way around that is to pass in, not the object itself, but its id" - https://www.toptal.com/emberjs/the-8-most-common-ember-js-developer-mistakes".
So you should do
hbs
{{#link-to 'team' organization.team.id}} Link to team management {{/link-to}}
route
model(params) {
return this.store.findRecord('team', params.team_id)
}
you can use modelFor('parent') method to get organization model.
like that
//app/routes/organization/admin/team.js
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
let organization = this.modelFor('parent');
return this.store.findRecord('team', params.team_id)
}
});
i think you wants to do something like that.
basically ember does not support nested routes.
I'm trying to do something like this in my routes:
this.route('products', { path: "/products/*choises"}, function() {
this.route('promotion', {path: "/promotion/*offers"});
});
product route:
offerPath: function(params){
this.transitionTo('product.promotion', params);
}
The problem is that it doesn't matter the promotion that I visit, the app thinks is part of the products route.
How can I do this? I need them to be nested.
Update:
You can use beforeModel(transition) hook in router to check what's in the url.
http://example.com/products/manufacturer-209/series-881/tag-17143/none/494822/flawless
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel(transition) {
console.log(transition.params.products.choises)
// if you use this url: http://example.com/products/manufacturer-209/series-881/tag-17143/none/494822/flawless
// console log would be: "manufacturer-209/series-881/tag-17143/none/494822/flawless"
}
});
At least you have the rest of the url so, you can filter out the important information and redirect with this.transitionTo() to the exact place.
You could have the following route:
http://example.com/products/123/promotions/456
or
http://example.com/products/awesome_souce/promotions/monday_deal
In the first case, your route would look like this:
this.route('product', { path: "/products/:product_id"}, function() {
this.route('promotion', {path: "/promotions/:promotion_id"});
});
In the second case, maybe like this:
this.route('product', { path: "/products/:product_name"}, function() {
this.route('promotion', {path: "/promotions/:promotion_name"});
});
Finally, your route handlers can download the proper models (example for the first case):
// app/routes/product.js
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
return this.store.findRecord('product', params.product_id);
}
});
---
// app/routes/product/promotion.js
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
// you can get access to the parent route model if you need for the api query
const product = this.modelFor('product');
return this.store.findRecord('promotion', params.promotion_id);
}
});
If you need only the param from the product route, instead of returning a whole record, for example you can just return params.product_name, so you will have access to a string with this.modelFor('product') in a subroute level.
Here is my router.js:
import Ember from 'ember';
import config from './config/environment';
var Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.route('category', {path: '/category/:category_id'});
this.route('listing', {path: '/listing/:listing_id'});
});
export default Router;
I'm trying to make it so that when the user selects an option from the navbar component, they will be redirected to the appropriate categories/category_id page. I am able to get the right ID into a variable in the navbar component but my this.transitionTo statement does not work. Here is the navbar-header.js:
import Ember from 'ember';
export default Ember.Component.extend({
model() {
return this.store.findAll('category');
},
actions: {
categorySelected: function(){
debugger;
var e = document.getElementById("categories");
var catId = e.options[e.selectedIndex].value;
//I have verified that catId contains the appropriate ID at this point.
//Where the error happens:
this.transitionTo('/category/' + catId);
}
}
});
What am I doing wrong?
EDIT: Heads up everyone, you can't do a transitionTo from a component, you have to use an action and send it back to the route. And as joshfarrent said it is supposed to be this.transitionTo('category', catId);
You need to pass the Id as a separate param in the transitionTo, and remove the slashes, like this:
this.transitionTo('category', catId);
See the transitionTo section of the Ember Route docs here.
I'd also recommend against using the HTML element's value to figure out which item has been selected, and rather do something like this on each action helper in your template:
{{action "categorySelected" VALUE}}
Just replace VALUE with the same numerical value that you were setting on the HTML element. This way, the value of the element will be passed to your categorySelected function as follows:
categorySelected: function(value) {
debugger;
this.transitionTo('category', value);
}
Finally, is there a reason you're not just using a {{link-to}} helper to achieve the same effect?
I am using ember cli with ember data and have been piecing together information but it still doesn't work. This all involves the Home model, route and template. I feel like I'm close but still no cigar. I took everything out except for the title to simplify it. According to documentation I've read, everything is as it should be.
here is my app.js
import Ember from 'ember';
import Resolver from 'ember/resolver';
import loadInitializers from 'ember/load-initializers';
import config from './config/environment';
Ember.MODEL_FACTORY_INJECTIONS = true;
var App = Ember.Application.extend({
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix,
Resolver: Resolver
});
loadInitializers(App, config.modulePrefix);
export default App;
Here is my home model:
import DS from 'ember-data';
export default DS.Model.extend({
title : DS.attr('string'),
});
Home.reopenClass({
FIXTURES :[
{
id: 1,
title: 'Sponge B',
},
{
id: 2,
title: 'John David',
},
]
});
Here is my home route:
import Ember from 'ember';
//import DS from 'ember-data';
export default Ember.Route.extend({
model: function(){
return this.store.find('Home');
},
});
This is my home template:
<div id="home">
{{#each}}
<p>{{title}}</p>
{{/each}}
</div>
{{outlet}}
Could someone please help me out?
I think it has something to do with my model hook.
Could this also be a controller issue. I generated a basic controller. Should I have generated an arrayController?. The home route when saved gives me this error message:
models/home.js: line 9, col 1, 'Home' is not defined. but when I define it there still is a problem. Am I supposed to ad a folder called adapters, then put a file in it called application.js, then ad export default DS.FixtureAdapter.extend(); . But when I do that it tells me DS is not defined
It looks like you are explicitly exporting your model as Home and then you are trying to find('home') which does not have the same letter case.
Ember will not automatically resolve the case for you. If you want to use Home as the model, you will need to call it in the same way every time.
You might even need to import Home from '../models/model-file'; if you want to call it from the route..
An easier thing to try would be to use the implicit export default Ember.Model.extend({}) and let ember-cli resolve the class using your file name.
http://www.ember-cli.com/#using-modules
Your template seems to be the issue. You reference an item property that isn't there:
<div id="home">
{{#each}}
<p>{{title}}</p>
{{/each}}
</div>
Also, you have some syntax problems with your model. Try this:
import DS from 'ember-data';
var Home= DS.Model.extend({
title : DS.attr('string'),
});
Home.reopenClass({
FIXTURES :[
{
id: 1,
title: 'Sponge B',
},
{
id: 2,
title: 'John David',
},
]
});
export default Home;
For the Fixture Adapter you are correct in adding the application.js to the adapters folder and import DS from 'ember-data'.
Also in Steve H's example of your home model, the definition of the FIXTURES is not correct. Try using:
Home.FIXTURES = [
...
];
Make sure the pathing is correct.