0
votes

this question is slightly related to How to display the “content” of an ObjectController? However, in the provided solution and all other examples I can find the controllers are always created explicitly. The nice thing about Ember.js is that the Route takes care of mostly everything. So I don't want to create the controller but want to bind it to a view:

{{view App.myview controllerBinding="App.applicationController"}}

You can see the complete example in this fiddle. The example is not that great because Ember usually sets the controller of a child view to its parent view.

In the end I need to know, how I can access a controller which is created by Ember from a view.

Thanks for any help!

Update: I provided the wrong fiddle or it did not save my changes. Here is the link to the right version: http://jsfiddle.net/ncaZz/1/

What should I provide in line 9 in the templates?

2

2 Answers

0
votes

From the view you can access the controller with

this.controller

If you need other controllers than your view controller you can use the needs in the viewcontroller:

App.DatasetEditController = Ember.ObjectController.extend({
  needs: ['mappingIndex']
});

and then use:

this.controller.mappingIndex
0
votes

You don't really need to bind to it. You can access the controller from the view by calling it like this.

this.get('controller');

Updated Answer:

You really should not have your click event inside your view. Your actions should either be in your controller or your router.

Your template should become

<span style="background-color: green" {{action doStuff}}>
    Click
</span>

and you should have a controller that should have this

App.MyController = Em.Controller.extend({
    needs: ['application'],
    doStuff: function(){
       this.get('controllers.application').foo();
    }
});

Also, the MyView and MyController should be capitalized, because when extending these items from ember that are not instances, and the capitalization is required. The view should only really have stuff in the didInsertElement that handles special things like any kind of jquery animations or initializing a date picker. But, the "ember way" is to have action in the router or controller.