ember.js - How to get the latest active item? -
the following returns active items.
activeitems: ember.computed('items.@each.status', { get() { return this.get('items').filter((item) => { return item.get('status') === 'active'; }); } }) // template {{#each activeitems |activeitems|}} {{activeitem.status}} {{/each}} all of above works. lets want create computed property picks out last activeitem. tried:
activeitem: ember.computed('activeitems', { get() { return this.get('activeitems').lastobject; } }), // template {{activeitem.status}} <-- returns nothing why , how can work?
i see 2 problems:
- your computed property has incorrect dependent key. relying on
activeitems, problem update whenactiveitemsproperty returns new object, not when contents updated. should watchingactiveitems.lastobject. lastobjectcomputed property, means might not able access without using ember'sget()function.
try this:
activeitem: ember.computed('activeitems.lastobject', { get() { return this.get('activeitems.lastobject'); } }) or, shorthand:
activeitem: ember.computed.reads('activeitems.lastobject')
Comments
Post a Comment