Search code examples
ember.jsember-cli

Items are sorted differently when one them is saved?


I'm showing downloads of my post. Upon clicking on them an action is executed to increment their download-count and issue the download itself.

When there are two download-items, I click on the first one, its download-count changes in the templates and it switches position with the second download-item. Why are they interchanged and how can I prevent it?

This is the template:

  {{#each download in post.downloads }}
    <p>
      <a {{ action "incDownload" download }}>
        {{ download.name }}
      </a> - {{ unbound download.downloadcount }} Hits
    </p>
  {{/each}}

This is the controller:

import Ember from 'ember';

export default Ember.ObjectController.extend({

  actions: {
      incDownload: function(obj) {
          obj.incrementProperty('downloadcount')
          obj.save()
        }
      }
    }
})

Solution

  • You aren't performing any explicit sorting so when the contents of the downloads array are changed their sorting may be changed as well. Use the SortableMixinto ensure a consistent sort order.