1
votes

I am using rails 4 with emberjs. I have an array of users that I get from the server. I need to display the last object on the UI. Also, the user can add new users to the array but only the last/latest-added user should be visible.

My ember version details are as

Ember      : 1.7.0-beta.1
Ember Data : 1.0.0-beta.8.2a68c63a
Handlebars : 1.3.0
jQuery     : 1.11.1

My current handlebars code is as. After adding the new user to users array, the new user details is displayed.

{{each users}}
  //display all the users here.
{{/each}}

What i want is something like this

{{#each users}}
  {{#if @last}} 
    //display user details
  {{/if}}
{{/each}}

I tried adding a property in the associated controller and removed the 'each' loop. I displayed the lastUser details which works. But if I add a new user, the UI still displays the previous user details.

lastUser: (->                                                                                                                             
  return @model.users.get('lastObject')
).property('@model.users')

How can this be acheived? Thanks.

2

2 Answers

2
votes

You can simply use the following:

{{users.lastObject.firstName}}
{{users.lastObject.lastName}}

or more cleanly:

{{#with users.lastObject}}
  {{firstName}}
  {{lastName}}
{{/with}}

With just redefines this within the block to the object provided--in this case the users.lastObject object. Your lastUser computed property is unnecessary because Ember enumerables already provide this for free!

1
votes

To watch additions to the users array, you need to watch the 'users.@each'

See the following documentation http://emberjs.com/guides/object-model/computed-properties-and-aggregate-data/