Why two router Emberjs error - ember.js

I have code emberjs router is below.I have two router App.CateProductRoute and App.ProductRoute
then
console.log("CateProduct"); not run function show console
console.log("Product"); run function show console
App = Ember.Application.create();
App.Router.map(function () {
//Product
this.resource('cateproduct', function () {
this.route('cateproduct', { path: 'cateproduct/:id' });
});
this.resource('product', function () {
this.route('product', { path: 'product/:id' });
});
})
App.IndexRoute = Ember.Route.extend({
model: function () {
return {};
}
});
App.CateProductRoute = Ember.Route.extend({
model: function () {
console.log("CateProduct"); not is run show
}
});
App.ProductRoute = Ember.Route.extend({
model: function () {
console.log(" Product"); is run show
}
});

Ember doesn't know that you want to camelCase your CateProduct Route and therefore instead of using your declared CateProduct Route, Ember is creating the missing Route --> CateproductRoute.
Conclusion: Rename your CateProduct Route (and all Controllers, Models, etc.) to Cateproduct or rename your resource to cateProduct (just rename the resource, the path can remain the same) and everything should work with CateProduct too :) - also, for this type of routing you won't need the inner routes - everything works without them pretty well
so, you either have:
App.Router.map(function () {
this.resource('cateProduct', {path: 'cateproduct/:id'});
this.resource('product', {path: 'product/:id'});
});
and keep your CateProduct named Route/Controller/Model
or you have:
App.Router.map(function () {
this.resource('cateproduct', {path: 'cateproduct/:id'});
this.resource('product', {path: 'product/:id'});
});
and rename your Route/Controller/Model to match Cateproduct

Related

Nested routes in ember engine routes.js?

How to define nested routes in ember routable engine? I can't navigate to beyond 2 trees. Like,
For example
All posts
Post
Comments
Comment
I can access
localhost:4200/posts/:postid/
But when I access
localhost:4200/posts/:postid/comments/:commentid
Its not rendering the content for comments template. But it doesn't show any error either.
In your terminal
$ ember g route posts
$ ember g route posts/post
$ ember g route posts/post/comments
$ ember g route posts/post/comments/comment
In your router.js, replace the contents by the following
Router.map(function(){
this.route('posts', function() {
this.route('post', {path: '/:post_id' }, function() {
this.route('comments', function() {
this.route('comment', {path: '/:comment_id'});
});
});
});
});
This is a solution, But what I prefer is , define an index sub-route in every main routes, for example ember g route posts/index and add it into your router.js like
this.route('posts', function() {
this.route('index', {path: '/'});
this.route('post', {path: '/:post_id'}, function() {
.....
.....
});
});
add an index sub route every time

In an Ember framework, How do I remove the Pod folder name from the URL

I'm using Ember POD syntax to organize my Ember application.
Is it possible to remove the pod directory from appearing in the browser URL? If so, how?
See picture.
This is my current router.js file
Router.map(function() {
this.route('main', function() {
this.route('about');
this.route('focus', function() {
this.route('show');
});
this.route('projects', function() {
this.route('show');
});
this.route('articles', function() {
this.route('show', {path: '/:id'});
});
this.route('contact');
});
});
Thanks.
Your main route can have a path of ''.
this.route('main', { path: '' }, function() {
this.route('about');
// etc...
}

Understanding Router Resources in Ember.js

Let's suppose my Ember.js App has a router that looks like this:
App.Router.map(function () {
this.resource('posts');
});
I understand from the guide that this means that I get two routes: PostsRoute, and PostsIndexRoute, and that PostsRoute is the parent.
Here's my PostsRoute:
App.PostsRoute = Em.Route.extend({
model: function () {
this.store.find('posts');
},
afterModel: function (posts) {
var promises = posts.map(function (post) {
return post.get('author'); // some async relationship
});
return Em.RSVP.Promise.all(promises);
}
});
And my PostsIndexRoute:
App.PostsIndexRoute = Em.Route.extend({
model: function () {
return this.modelFor('posts');
}
});
Since my PostsRoute is the parent of PostsIndexRoute, I'm expecting that it's model hooks are always resolved, even when transitioning from somewhere else in my application to 'posts' (e.g. via a transitionTo or link-to). This does not seem to be the case. What exactly is the relationship between a <Resource> and <Resource>Index route? I'm a bit confused as to why you'd need both.
In order to have Ember automatically call the PostsIndexRoute's model hook you have to declare your resource with a second argument. Having a second argument indicates that it should have child routes (index being the one provided by default).
App.Router.map(function(){
this.resource('posts', function(){});
})
Here's a bin that demonstrates the behaviour.

Having trouble getting my model set up

I have the following routes :
this.resource('categories', function() {
this.route('add');
this.resource('category', {path: ':category_id'}, function() {
this.route('edit', {path: "/edit"});
this.resource('products', function(){
this.route('add');
this.resource('product', {path: ':product_id'});
});
});
});
Everything works except the edit route. For some weird reason, my model doesn't get initialized.
App.CategoryRoute = Ember.Route.extend({
model: function (params) {
console.log(this.store.getById('category', params.category_id)); // Returns what I want
return this.store.getById('category', params.category_id);
},
//other stuff
});
After couple of trial/errors, I found out that my CategoryEditRoute overwrite my model. I'm not 100% sure about this statement, but it looks like it. If I try to set up my model in this route, I can't access my params... without my params I can't know what model to load!!!
App.CategoryEditRoute = Ember.Route.extend({
model: function (params) {
console.log(params); // Result in: Object {}
return this.store.getById('category', params.category_id); // Undefined
},
// Other stuff
)}
Thanks for taking the time to help.
App.CategoryEditRoute = Ember.Route.extend({
model: function() {
return this.modelFor('category')
},
});
This will use the model that was resolved in the category resource.
Do also note that routing in Ember is different from routing in Rails, for example. You typically nest routes if the views are nested. There is nothing wrong with (and may actually make things easier and simpler) doing this:
this.route('categoryEdit', {path: ':category_id/edit'})

Issue making dynamic segment available to nested route in Ember.js

I'm having an issue making a nested route available in Ember.js
App.Router.map(function() {
this.resource('home', { path: '/' });
this.resource('form', { path: '/forms/:form_id' }, function() {
this.route('newRecord');
});
I have the parent route...
App.FormRoute = Ember.Route.extend({....
Which is working.
Next I have...
App.FormNewRecordRoute = Ember.Route.extend({
afterModel: function() {
this.set('newRecord', this.modelFor('form');
}
});
Normally I get the URL with something like localhost/forms/9087
With the child route I'm getting localhost/undefined/newRecord
I'm guessing that 'localhost/undefined/newRecord' is coming from something like {{#link-to 'newRecord'}}. It should probably be {{#link-to 'form.newRecord'}}.