Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering Ember ArrayProxy

How would you filter the result of an ArrayProxy? I've tried slice, filter, rejectBy, all leading to no result in the view. I imagine it's because the data isn't available yet, but usage of then(...) hasn't panned out either. Any thoughts?

shownEvents: function(){
    return Em.ArrayProxy.createWithMixins(Em.SortableMixin, {
        content: this.get('shownUser.events'),
        sortProperties: ['eventTime.startTime', 'eventTime.endTime'],
        sortAscending: true
    });
  }.property("shownUser"),

I've reviewed quite a few articles similar to this but haven't found anything that quite works.

Can I add an additional computed property to an Ember ArrayProxy?

like image 588
Michael Avatar asked Feb 23 '26 12:02

Michael


1 Answers

You can filter ArrayProxy by passing in a function to filter and returning true for the values that should pass the filtering test.

Something like:

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return { pets: [ { type: 'dog'}, { type: 'cat'}, { type: 'fish'}] };
  }
});

App.IndexController = Ember.ObjectController.extend({
  myPets: function(){
    return Em.ArrayProxy.createWithMixins(Em.SortableMixin, {
      content: this.get('pets'),
      sortProperties: ['type'],
      sortAscending: true
    }).filter(function(item){ return item.type.length === 3});
  }.property("pets"),
});

Works here

Fill free to ignore this if you have tried this already ;)

like image 171
Kalman Avatar answered Feb 26 '26 13:02

Kalman