Eben Roux
Eben Roux

Reputation: 13246

Ember.js RC1 - controller 'needs' another that does not yet exist

My routing structure:

App.ready = function() {
    App.Router.map(function() {
        this.resource('contacts', function() {
            this.resource('contact', function() {
            });
        });
    });
}

Now in my contactsController I respond to and add action that transitions to the contact route. I would then like to call the add method on my contactController.

I have placed the needs: ['contact'] on my ContactController but then I get this message:

<App.ContactsController:ember197> needs controller:contact but it does not exist

When I use controllerFor (which is deprecated) I also get an error:

this.controllerFor('contact').add();

So Ember.js RC1 appears to only create the controllers (and other related instances) once one actually transitions to the appropriate route.

Is there a way around this.

Upvotes: 1

Views: 634

Answers (1)

Mike Grassotti
Mike Grassotti

Reputation: 19050

So Ember.js RC1 appears to only create the controllers (and other related instances) once one actually transitions to the appropriate route.

Interesting - I had thought ember generated controllers earlier but guess not.

Is there a way around this?

Workaround is to define App.ContactController manually. Something like this will work:

App = Ember.Application.create({});

App.Router.map(function() {
    this.resource('contacts', function() {
        this.resource('contact', function() {
        });
    });
});

App.ContactController = Ember.Controller.extend({
  add: function() {
    alert('App.ContactController.add() was called!');
  }
});

App.ContactsController = Ember.Controller.extend({
  needs: ['contact'],
  add: function() {
    this.get('controllers.contact').add();
  }
});

http://jsbin.com/osapal/1/edit

Upvotes: 2

Related Questions