Andreas Köberle
Andreas Köberle

Reputation: 111092

How to reference to emberJS controller from another view

I'm using ember tools for my app. Now I want to add a TextField to trigger a search in my controller, like in this example. This is the controller and view:

ProductsController:

var ProductsController = Ember.ArrayController.extend({
  search: function(query) {
    console.log(query);
  }
});

module.exports = ProductsController;

SearchFieldView:

var SearchFieldView = Ember.TextField.extend({
  insertNewline: function() {
    var query = this.get('value');
    App.ProductsController.search(query);
  }
});
module.exports = SearchFieldView;

But whenever the textfield is changing I've got the error that the App.ProductsController has no method search. So I've got the feeling it is not the one I have created but the generated one.

Upvotes: 2

Views: 666

Answers (2)

Raj
Raj

Reputation: 342

You can use this.get('controller') to get the current controller instance. or if u need a another controller instance you can use this.get('controller').get('controllers.anothercontroller').. JSBin

Hope it helps.

Upvotes: 1

Andreas Köberle
Andreas Köberle

Reputation: 111092

Ok, I've got it. App.ProductsController isn't the instance of the actual controller and as all views under route have the same controller its just this.get('controller')

var SearchFieldView = Ember.TextField.extend({
  insertNewline: function() {
    var query = this.get('value');
    this.get('controller').search(query);
  }
});
module.exports = SearchFieldView;

Upvotes: 0

Related Questions