rob
rob

Reputation: 3012

Ember controller dependency resolution not working on direct links?

I use dependency resolution between controllers in Ember and it works fine as long as you navigate down the route. But If you directly link to the view which has a controller which has a dependency, it does not work with the following error message:

needs [ controller:productsDetailIndex ] but it could not be found

The according controller:

Pd.ProductsDetailOrderController = Ember.Controller.extend({
    needs: "productsDetailIndex"
});

Router:

Pd.Router.map(function () {

    this.resource('products', { path: '/products' }, function () {
        this.resource('products.detail', { path: '/:product_id' }, function() {
            this.route('order');
        });

    });
});

Pd.ProductsIndexRoute = Ember.Route.extend({
    model: function () {
        return this.store.all('product');
    }
});

Pd.ProductsDetailIndexRoute = Ember.Route.extend({
    model: function(params){
        return this.store.getById('product', params.product_id);
    }
});

Pd.ProductsDetailOrderRoute = Ember.Route.extend({

});

So, if I go to /products/3 and click on link which opens /products/3/oder it's all good. But If I directly link to /products/3/order the above error comes up.

Upvotes: 0

Views: 85

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

If your logic is dependent on a controller that isn't in its path you've got an architectural problem. In your particular case the resource is in its path, so you should move whatever logic you have in the index controller into the 'productsDetail' controller.

Pd.ProductsDetailRoute = Ember.Route.extend({
    model: function(params){
        return this.store.getById('product', params.product_id);
    }
});

Pd.ProductsDetailIndexRoute = Ember.Route.extend({
    model: function(params){
        return this.modelFor('productDetails');
    }
});

Upvotes: 1

Related Questions