Ryan Haywood
Ryan Haywood

Reputation: 549

How is Ember handling this controller .property()

I am displaying a list of Document titles on the site's sidebar area. To achieve this I created a documents property on the App.SidebarController

Document = require '../models/document'

SidebarController = Ember.Controller.extend
  documents: (->
    Document.find()
  ).property()

module.exports = SidebarController

This works fine, when I create a new Document in my application, this sidebar property get's updated automatically and I don't know why. It works no matter what I put in the .property() function. e.g.

.property('Document')
.property('App.Document')

Can somebody explain how this works? What would be the correct thing for the property() function to observe?

Upvotes: 0

Views: 125

Answers (1)

mavilein
mavilein

Reputation: 11668

So you are basically saying that is works, but you would like to know why.

Here is my guess: When your controller gets rendered, your computed property gets computed for the first time and an instance of an array is returned. When you create a new Document, i think this very same array is updated (= Document is added). So it does work despite the fact, that you do not specify a dependent key on property(). This is because the right array is already in place. As i said just a guess, but this seems like an understandable explanation.

And what would be the correct thing to do? IMHO one should not use such Data Access methods directly in a computed property. Instead you should call this Data Access method inside a Route and then assign it to the controller. This way you make sure, that the find() method gets executed when needed.

Upvotes: 2

Related Questions