GogromaT
GogromaT

Reputation: 511

EmberJS 2 route dynamic segment in not nested routes undefined

Really new to ember and trying to setup basic (in my mind) routes.

I have calendars resource and I want to display individual calendars.

My app/router.js has the following:

this.route('calendar', {path: 'calendars/:calendar_id'}, function () {
    this.route('show');
    this.route('edit');
});
this.route('calendars', function(){
    this.route('create');
});

Folders are as following:

app/routes: [
  calendars: [create, index],
  calendar: [edit, show]
]
app/templates: [
  calendars: [create, index]
  calendar: [edit, show]
]

In app/routes/calendar/show.js:

import Ember from 'ember';
export default Ember.Route.extend({
    model(params) {
        return this.store.findRecord('calendar', params.calendar_id);
    }
});

Problems start when I go to http://SERVER/calendars/5/show (5 is a :calendar_id part, SERVER is what hosts ember app) :

How does Ember make POST request if model() hook is missing from route, I have no controllers difined. Also how do I fix it so that it works? ;p

Edit [2]:

I am trying this now and I think it kinda works, but looks ugly:

this.route('calendars',{ path: '/calendars'}, function(){
    this.route('create');
});
this.route('calendar', { path: '/' }, function () {
    this.route('show', { path: '/calendars/:calendar_id/show' });
    this.route('edit', { path: '/calendars/:calendar_id/edit' });
});
this.route('index', { path: ''});

Upvotes: 0

Views: 100

Answers (1)

Jonathan Jansen
Jonathan Jansen

Reputation: 133

Ember is smart enough to generate a default route if you do not create one, and a default model if you do not create a model function.

It does this based on the routes name ie if your route is "calendar" it generates a model function based on the "calendar" model.

Try explicitly define your route path with the parameters as per ember docs: https://guides.emberjs.com/v2.9.0/routing/defining-your-routes/

this.route('calendar', function () {
    this.route('show', { path: '/:calendar_id/show' });
    this.route('edit', { path: '/:calendar_id/edit' });
    this.route('create');
});

Upvotes: 1

Related Questions