Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember 1.0.0 - can no longer access controller from view?

Tags:

ember.js

I'm using a render helper inside a template, which renders a searchbox with a typeahead.

Essentially (code removed for brevity):

script(type='text/x-handlebars', data-template-name='index')
    {{render search}}

script(type='text/x-handlebars', data-template-name='search')
    {{view App.TaggableInput valueBinding="searchText"}}

Which gives me a SearchController separated from the IndexController.

Inside App.TaggableInput I'm grabbing searchController to do some checking on the keyUp event:

App.TaggableInput = Ember.TextField.extend({
    keyUp: function(e){
        var controller = this.get('controller');
        // Do stuff with the controller
    }
});

On Ember RC7, I can access the controller inside theview as you'd expect with this.get('controller').get('searchText').

However in Ember 1.0.0 this.get('controller') returns the view, and whatever I do I can't get searchController.

I can't find any related info on the ember website regarding what's changed or what I'm supposed to do... for now I'm sticking with RC7.

Any ideas? I've spent hours on it this morning and can't figure it out. Thanks.

UPDATE: Fixed!

I swapped out this.get('controller') for this.get('targetObject') and it works as before. Had a peruse through a recent commit in ember source to find it...

Thanks for your suggestions guys!

like image 373
Iest Avatar asked Sep 02 '13 10:09

Iest


2 Answers

I guess that in your code

App.TaggableInput = Ember.TextField.extend({
    keyUp: function(e){
        var controller = this.get('controller');
        // Do stuff with the controller
    }
});

this line

 var controller = this.get('controller');

gets the controller associated to your (subview)

Try to use this line instead to access the route's controller:

 var controller = this.get('parentView.controller');
like image 145
splattne Avatar answered Nov 10 '22 13:11

splattne


Currently, the {{render}} helper takes 2 arguments, the first is the context, the second is the model. I recommend using this method and following the naming convention for the model's controller rather than setting the controller explicitly.

You can find the docs here: http://emberjs.com/guides/templates/rendering-with-helpers/#toc_the-code-render-code-helper

Accessing controllers from views was also being tracked in this discussion: https://github.com/emberjs/ember.js/issues/1712#issuecomment-31183940

like image 3
genkilabs Avatar answered Nov 10 '22 15:11

genkilabs