Passing content into a view


In my template I have something like:

{{#each controller}}    
    {{view App.FancyViewWidget contentBinding="this"}}

In my view, I need to access that controller instance once the element has been added.

App.FancyViewWidget = Ember.View.extend({
    tagName: 'canvas',
    didInsertElement: function() {

My view’s content is always null, so I am doing something wrong. How can you pass in a model and make it accessible by the view?

Many thanks.


To pass a model to controller you can use Routes setupController

I went a long time struggleling with Ember until I embraced its parts. Imho it’s easier to use Ember as a whole versus is parts. I usually start with my templates, then routes, then models->controllers… lastly views that pass on events to controllers that manipulates models.


Thanks, but I’m not looking to pass a model to a controller. My issue is that I still don’t know where in my array of models I am when I call the view. In other words, I need a specific context (my iteration within the array), not the general context of the route’s model.

My view widget does have access to the route’s model, you’re right, but in this case I need to pass this from my #each loop.


@mrcwinn, how are you setting up the controller referenced in {{#each controller}}?


Thanks for the reply. My route defines a model, App.Benchmark.find(). I use setupController to set the content to my model, so {{#each controller}} is referencing App.Benchmark.

The view I’m inserting into the template is simply a canvas tag. However, it’s important in the didInsertElement (or somewhere) I be able to reference the specific instance of my array of models in each. So in other words, the flow is something like:

each benchmark in benchmarks -> view MyWidget benchmark

Right now, within that each loop, I pass this in my widget’s contentBinding yet I can’t seem to be able to access it!

Thanks, Chris


Check if the view actually has the contentBinding property on it. If it does then the content in that binding is probably undefined rather than it being an error. Also to access the controller from the view you can just do this.get('controller').


In the interest of a definitive answer being recorded, it turns out there was no change necessary to my view. According to Ember docs, a view’s context is set automatically (though it can be overrided). In this case, the context was also set to the iterated object.

So in my view’s didInsertElement I can simply call this.get('context').get('name') to retrieve the name of my object at that point in the array controller. Easy!