NinGen ShinRa
NinGen ShinRa

Reputation: 651

Calling Controller method within view Ember.js

I have a controller in Ember like so:

App.TasksController = Ember.ArrayController.extend({
    search: function(term){ ... }
})

And I have the relative view, with a custom text field, as such:

App.TasksView = Ember.View.extend({
    searchField: Ember.TextField.extend({
        keyUp: function(){ this.get('controller').search() }
    })
})

I however get an error saying that there is no such method.

I was wondering:

  1. How can I correctly call the method defined in the controller from the view?
  2. How can I debug which is the currently active controller? If I console.log(this.get('controller')) I get an object, but it is very confusing to navigate and to understand exactly which controller is that.

Upvotes: 1

Views: 914

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

the scope of this on the text field isn't the same scope as the tasksview, so it doesn't have access to the controller.

Honestly a better way to handle this is to bind the textfield value to something and add an observer to it. When it changes fire off a search (or probably better would be to debounce it so you aren't firing off requests every single character they type)

http://emberjs.jsbin.com/IRAXinoP/3/edit

Upvotes: 1

Related Questions