Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4837

Ember.computed.sort not invalidating cache

$
0
0

@andrew1 wrote:

I'm sorting an array of objects in my app and some of the parameters are sorted alphabetically, while others are sorted as integers:

_sortedContent: Ember.computed(
  '_alphaSortedContent',
  '_intSortedContent',
  '_sortKey',
  '_sortOrder',
  function () {
    console.log('_sortedContent');
    let columns = Ember.get(this, 'columns');
    let sortKey = Ember.get(this, '_sortKey');
    let selectedColumn = Ember.A(columns).findBy('propertyName', sortKey);

    if (typeof selectedColumn === 'undefined' || typeof sortKey === 'undefined') {
      return Ember.get(this, 'content');
    }

    switch (Ember.get(selectedColumn, 'sortType')) {
      case 'int':
        console.log('_intSortedContent');
        return Ember.get(this, '_intSortedContent');
      default:
        return Ember.get(this, '_alphaSortedContent');
    }
  }
),

_alphaSortedContent: Ember.computed.sort('content', '_sortDefinition'),

_intSortedContent: Ember.computed.sort('content',
  function (a, b) {
    let aSortKey = parseInt(Ember.get(a, Ember.get(this, '_sortKey')));
    let bSortKey = parseInt(Ember.get(b, Ember.get(this, '_sortKey')));
    let sortOrder = Ember.get(this, '_sortOrder');

    if (aSortKey > bSortKey) {
      console.log(`aSortKey:${aSortKey}, bSortKey:${bSortKey}, sortOrder:${sortOrder}`);
      console.log('greater than');
      return (sortOrder === 'asc') ? 1 : -1;
    } else if (aSortKey < bSortKey) {
      console.log(`aSortKey:${aSortKey}, bSortKey:${bSortKey}, sortOrder:${sortOrder}`);
      console.log('less than');
      return (sortOrder === 'asc') ? -1 : 1;
    }
    return 0;
  }
),

The _alphaSortedContent works fine and I can toggle between asc and desc (_sortDefinition is a computed property on _sortKey and _sortOrder so it changes when _sortOrder changes). But, the _intSortedContent won't update when _sortOrder changes from asc to desc. _sortedContent is being fired and it calls _intSortedContent but it looks like _intSortedContent is then returning a cached copy rather than recalculating.

What's the best way to invalidate the cache of intSortedContent?

Thanks!

Posts: 5

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4837

Trending Articles