Should a computed property be declared in a model or controller?

Availability of the following user model:

Sks.User = DS.Model.extend
  firstName: DS.attr("string")
  lastName: DS.attr("string")

where should the computed property 'fullName' be declared?

  fullName: Ember.computed(->
    firstName = @get("firstName")
    lastName = @get("lastName")
    firstName = ""  if firstName is `undefined`
    lastName = ""  if lastName is `undefined`
    lastName + " " + firstName
  ).property("firstName", "lastName")

Should it be in the "UserController" or directly in the model? The Ember documentation says that fields used only in a session should be written to controllers. But the problem is that I could not access the "fullName" in the Index template:

Sks.IndexController = Ember.Controller.extend
  needs: ['users']

Here 'fullName' was unavailable (declared in the controller)

{{#each user in controllers.users}}
  <li>{{user.fullName}}</li>
{{/each}}

But this is when he is in the model.

+5
source share
1 answer

In this case, I think the model is the right place for the computed property, because it makes sense if you have the firstname and lastname attributes.

, , , , "fullName", ( )

+5

All Articles