0

When trying to access the model of a controller when creating a computed property on the controller, I get the following error:

model.uniqBy is not a function

app/controller/ticket.js

export default Ember.Controller.extend({
  statuses: Ember.computed('model', function() {
      var model = this.get('model');
      return model
              .uniqBy('status')
              .map(function(i) { return i.status; })
              .toArray();
  }),
});

The model I'm giving to the controller is a collection returned from this.store.findAll('ticket');, but trying to iterate through it seems to be causing the above error. Is the collection given to the model not supposed to be an Ember.Enumerable object? Should I be trying to access the collection via the DS.Store (in which case I don't understand the need to pass a model to the controller)?

a7omiton
  • 1,597
  • 4
  • 34
  • 61
  • What are you trying to get? Array of unique values of status fields which is stored in collection of tickets? – Mirza Memic Jun 18 '16 at 17:07
  • You should be using 2.7 earlier version, In ember 2.7 introduced uniqBy macro. http://emberjs.com/blog/2016/06/08/ember-2-6-and-2-7-beta-released.html – Ember Freak Jun 19 '16 at 14:09
  • @kumkanillam I'm aware of that macro, it is documented but not implemented and is planned to be available in 2.7, which is in beta. I prefer to stay with the stable releases though and found the answer thanks to help in this question. Thanks! – a7omiton Jun 21 '16 at 01:51

1 Answers1

1

Ember.computed.uniqBy

A computed property which returns a new array with all the unique elements from an array, with uniqueness determined by specific key

Please try this instead for your computed property

statuses: Ember.computed.uniqBy('model', 'status')

EDIT

You can use ember computed map on this property to fine tune your array if needed, for example like this

status: Ember.computed.map('statuses', function(status, index)
  return status.toUpperCase() + '!';
})

Another way is that computed property uses dynamic aggregate syntax as described here

https://guides.emberjs.com/v2.6.0/object-model/computed-properties-and-aggregate-data/

so Ember.computed('model.@each.status', function()

Hope it helps

Mirza Memic
  • 862
  • 5
  • 9
  • Thanks but this would return an array of objects, I need to return an array of strings (eg ['done', 'waiting']) Also i don't think the problem is ember computed, it may be the way i'm reading the model? If it's a collection of tickets then i'm sure i should be able to call Ember.Enumerable methods on it, but it doesn't seem to be the case – a7omiton Jun 18 '16 at 17:18
  • Why not create another computed property that depends on statuses and use map function? so statusesAsStrings: Ember.computed('statuses', function()....etc here you do not need to filter them since they are already profiteered with unique statuses – Mirza Memic Jun 18 '16 at 17:21
  • Because I shouldn't need to :P – a7omiton Jun 18 '16 at 17:23
  • Well not sure what you mean. If you have two properties it is much cleaner. You can do this like you started but your computed property cannot depend on model. What you need to do is dependency on each model item like this Ember.computed('model.@each.status'...hope it helps – Mirza Memic Jun 18 '16 at 17:30
  • Ah the '@each' selector! That's exactly what I was looking for, thank you :). If you like you can update the answer to use it and i'll accept and close – a7omiton Jun 18 '16 at 17:40