jeudi 27 août 2015

Ember.js correct syntax for consecutive computed properties

I have the following code in my controller. It first filters a set of results based on a search property, and then orders the results.

results: function() {

    var searchTerm = this.get('searchTerm'),
        results = this.get('content');

    if (searchTerm) {
      results = results.filter(function(item) {
        return item.get('name').toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
      });
    }

  }.property('content', 'searchTerm', ),

sortedResults: Ember.computed.sort('results', 'sortProperties'),
    
sortProperties: function() {
    var order = this.get('ordering');
    if (order === 0) {
      return ['name:asc'];
    } else if (order === 1) {
      return ['name:desc'];
    }
  }.property('ordering'),

The template loops over sortedResults:

{{#each sortedResults as |job|}}
    {{job-table-row model=job}} 
{{/each}}

The above works fine. I would like to finally subject sortedResults to a slice function to show a limited number of results. I have tried the following:

slicedResults: function() {
    var sortedResults    = this.get('sortedResults');
    return sortedResults.slice(0,10);
}.property('sortedResults'),

Then looping over slicedResults:

{{#each slicedResults as |job|}}
    {{job-table-row model=job}} 
{{/each}}

In this case, no results are returned. What would be the correct way to use the sorted results in a slice function and then output the result to the template?




Aucun commentaire:

Enregistrer un commentaire