# Passing content into a view

**URL:** <https://discuss.emberjs.com/t/passing-content-into-a-view/1048>\
**Category:** Uncategorized\
**Created:** [April 20, 2013, 2:42pm UTC](https://discuss.emberjs.com/t/passing-content-into-a-view/1048 "2013-04-20T14:42:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrcwinn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mrcwinn/32/14271_2.png) [@mrcwinn](https://discuss.emberjs.com/u/mrcwinn)\
**Post date:** [April 20, 2013, 2:42pm UTC](https://discuss.emberjs.com/t/passing-content-into-a-view/1048/1 "2013-04-20T14:42:41Z")

</div>

In my template I have something like:

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

```

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() {
        console.log(this.get('content'));
    ...

```

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.

---

<div class="post-metadata">

**Author:** ![quangv](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/quangv/32/14594_2.png) [@quangv](https://discuss.emberjs.com/u/quangv)\
**Post date:** [April 20, 2013, 3:02pm UTC](https://discuss.emberjs.com/t/passing-content-into-a-view/1048/2 "2013-04-20T15:02:14Z")

</div>

To pass a model to controller you can use Routes setupController [http://emberjs.com/guides/routing/setting-up-a-controller/](http://emberjs.com/guides/routing/setting-up-a-controller/)

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.

---

<div class="post-metadata">

**Author:** ![mrcwinn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mrcwinn/32/14271_2.png) [@mrcwinn](https://discuss.emberjs.com/u/mrcwinn)\
**Post date:** [April 20, 2013, 4:06pm UTC](https://discuss.emberjs.com/t/passing-content-into-a-view/1048/3 "2013-04-20T16:06:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![panoptic](https://avatars.discourse-cdn.com/v4/letter/p/e19adc/32.png) [@panoptic](https://discuss.emberjs.com/u/panoptic)\
**Post date:** [April 20, 2013, 4:55pm UTC](https://discuss.emberjs.com/t/passing-content-into-a-view/1048/4 "2013-04-20T16:55:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrcwinn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mrcwinn/32/14271_2.png) [@mrcwinn](https://discuss.emberjs.com/u/mrcwinn)\
**Post date:** [April 20, 2013, 6:36pm UTC](https://discuss.emberjs.com/t/passing-content-into-a-view/1048/5 "2013-04-20T18:36:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BlakeWilliams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/blakewilliams/32/14272_2.png) [@BlakeWilliams](https://discuss.emberjs.com/u/BlakeWilliams)\
**Post date:** [April 20, 2013, 9:17pm UTC](https://discuss.emberjs.com/t/passing-content-into-a-view/1048/6 "2013-04-20T21:17:40Z")

</div>

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')`.

---

<div class="post-metadata">

**Author:** ![mrcwinn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mrcwinn/32/14271_2.png) [@mrcwinn](https://discuss.emberjs.com/u/mrcwinn)\
**Post date:** [April 21, 2013, 2:17pm UTC](https://discuss.emberjs.com/t/passing-content-into-a-view/1048/7 "2013-04-21T14:17:48Z")

</div>

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!
