# Trouble with computed property

**URL:** https://discuss.emberjs.com/t/trouble-with-computed-property/1068
**Category:** Uncategorized
**Created:** [April 23, 2013, 3:52am UTC](https://discuss.emberjs.com/t/trouble-with-computed-property/1068 "2013-04-23T03:52:08Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![reybango](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/reybango/32/14235_2.png) [@reybango](https://discuss.emberjs.com/u/reybango)
#### Post date: [April 23, 2013, 3:52am UTC](https://discuss.emberjs.com/t/trouble-with-computed-property/1068/1 "2013-04-23T03:52:08Z")

</div>

Hey all,

Having trouble with a computed property. It’s a simple one that should compare the value of title and only display a specific one in the template.

Seems like there’s an issue with the expression in isTitle().

This fiddle displays all the data and doesn’t have the computed property (it’s a clean slate):

> **[Edit fiddle - JSFiddle - Code Playground](http://jsfiddle.net/reybango/nswcu/)**
>
> Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.

This fiddle has the computed property isTitle and does a check for “{{#if isTitle}}”:

> **[Edit fiddle - JSFiddle - Code Playground](http://jsfiddle.net/reybango/nswcu/1/)**
>
> Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.

Would appreciate some help on this.

Thanks.

---

<div class="post-metadata">

### Author: ![guilhermeaiolfi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/guilhermeaiolfi/32/14264_2.png) [@guilhermeaiolfi](https://discuss.emberjs.com/u/guilhermeaiolfi)
#### Post date: [April 23, 2013, 12:06pm UTC](https://discuss.emberjs.com/t/trouble-with-computed-property/1068/2 "2013-04-23T12:06:53Z")

</div>

Hey,

I think this computed property belongs to the item controller. Try using an itemController for creating a controller for each item in your {{#each}} like so:

`{{#each item in model itemController="item"}}`

and create a `ItemController` and put your `isTitle` there.

> **[Edit fiddle - JSFiddle - Code Playground](http://jsfiddle.net/nswcu/2/)**
>
> Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.

---

<div class="post-metadata">

### Author: ![reybango](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/reybango/32/14235_2.png) [@reybango](https://discuss.emberjs.com/u/reybango)
#### Post date: [April 23, 2013, 1:14pm UTC](https://discuss.emberjs.com/t/trouble-with-computed-property/1068/3 "2013-04-23T13:14:10Z")

</div>

Thanks for the reply. I see that it works but Item is simply a model that’s associated to Index route. I always thought that you created controllers associated to routes and that the model had no awareness of the controller using it.

Since I associated the Item model to the Index route via the model hook, I assumed that creating an IndexController subclass and adding the isTitle computed property there made the most sense.

Am I wrong in this assumption?

---

<div class="post-metadata">

### Author: ![guilhermeaiolfi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/guilhermeaiolfi/32/14264_2.png) [@guilhermeaiolfi](https://discuss.emberjs.com/u/guilhermeaiolfi)
#### Post date: [April 23, 2013, 1:53pm UTC](https://discuss.emberjs.com/t/trouble-with-computed-property/1068/4 "2013-04-23T13:53:43Z")

</div>

Item model doesn’t know about the ItemController. ItemController is a wrapper for your model that will handle application logic for that item. For example: selection, is expanded or not, is visible.

I don’t know exactly what `isTitle` is meant for (the name is too generic to know). But it seems that it will say something about the state of your view and not about your model (although it can and uses data in your model).

---

<div class="post-metadata">

### Author: ![reybango](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/reybango/32/14235_2.png) [@reybango](https://discuss.emberjs.com/u/reybango)
#### Post date: [April 23, 2013, 1:57pm UTC](https://discuss.emberjs.com/t/trouble-with-computed-property/1068/5 "2013-04-23T13:57:51Z")

</div>

But since the model is associated to the Index route via the model hook, shouldn’t the Index controller act as the wrapper you’re referring to?

And isTitle is isn’t anything special. It’s just test code to get a better understanding of computer properties. In this case, the logic behind it is simply meant to determine if the title matches the one I’ve explicitly specified.

---

<div class="post-metadata">

### Author: ![guilhermeaiolfi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/guilhermeaiolfi/32/14264_2.png) [@guilhermeaiolfi](https://discuss.emberjs.com/u/guilhermeaiolfi)
#### Post date: [April 23, 2013, 2:10pm UTC](https://discuss.emberjs.com/t/trouble-with-computed-property/1068/6 "2013-04-23T14:10:51Z")

</div>

IndexController is a ArrayController (or it should be, since it’s showing a list of entries) and is a wrapper for that array. ItemController is a ObjectController, it acts as a wrapper for each of those items in the model’s array.

You could even take the `@each` in the `isTitle` from ItemController and it would work.

`isTitle` doesn’t work in IndexController basically because it’s an array controller. `this.get( 'title' );` will try to get title from the array (and not from each item in that array).

It should be easier to access the current changed item when “observing” items using `@each` though.

---

<div class="post-metadata">

### Author: ![reybango](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/reybango/32/14235_2.png) [@reybango](https://discuss.emberjs.com/u/reybango)
#### Post date: [April 23, 2013, 3:49pm UTC](https://discuss.emberjs.com/t/trouble-with-computed-property/1068/7 "2013-04-23T15:49:39Z")

</div>

Really appreciate the help. You were right on target. I also worked with Yehuda on this and he gave me an almost identical solution.

[http://jsbin.com/ahujuq/1/edit](http://jsbin.com/ahujuq/1/edit)

🙂

---

<div class="post-metadata">

### Author: ![wycats](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/wycats/32/14179_2.png) [@wycats](https://discuss.emberjs.com/u/wycats)
#### Post date: [April 24, 2013, 7:23pm UTC](https://discuss.emberjs.com/t/trouble-with-computed-property/1068/8 "2013-04-24T19:23:33Z")

</div>


