Trouble with computed property


#1

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): http://jsfiddle.net/reybango/nswcu/

This fiddle has the computed property isTitle and does a check for “{{#if isTitle}}”: http://jsfiddle.net/reybango/nswcu/1/

Would appreciate some help on this.

Thanks.


#2

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.

http://jsfiddle.net/nswcu/2/


#3

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?


#4

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


#5

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.


#6

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.


#7

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

:slight_smile:


#8