Michael Villeneuve
Michael Villeneuve

Reputation: 4033

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.

Upvotes: 0

Views: 24

Answers (1)

Peter Wong
Peter Wong

Reputation: 491

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'})

Upvotes: 1

Related Questions