0

I am using Ember with jQuery UI sortable and it works fine until I want to delete some item after sort. This questio is allready answerd on stackoverflow but that solution does not work for me. I am using Ember 2.6.

Component template

<ui id="department_roles">
    {{#each sortedList as |item|}}
        {{item.id}}
            <li data-id="{{item.id}}"  role_id="{{item.id}}" class="ui-state-default role_droppable">
                <span>
                    {{item.name}}
                </span>

                <button {{action "removeItem" item.id}}>Remove</button>
            </li>
    {{/each}}
</ui>

Component, The sorting works fine and after delete action item is deleted from array but DOM is incorrect.

    import Ember from 'ember';

export default Ember.Component.extend({
    list: Ember.ArrayProxy.create({content: Ember.A()}),
    init: function () {
        this._super();
        this.get('list').pushObject({
            id: 1,
            index: 1,
            name: 'name1'
        });
        this.get('list').pushObject({
            id: 2,
            index: 2,
            name: 'name2'
        });
        this.get('list').pushObject({
            id: 3,
            index: 3,
            name: 'name3'
        });
        this.get('list').pushObject({
            id: 4,
            index: 4,
            name: 'name4'
        });
        this.get('list').pushObject({
            id: 5,
            index: 6,
            name: 'name5'
        });
    },
    sortProps: ['index:asc'],
    sortedList: Ember.computed.sort('list', 'sortProps'),
    updateSortedOrder(indices) {
        this.beginPropertyChanges();
        let tracks = this.get('list').forEach((item) => {
            var index = indices[Ember.get(item, 'id')];
            if (Ember.get(item, 'index') !== index) {
                Ember.set(item, 'index', index);
            }
        });
        this.endPropertyChanges();
    },
    didRender: function () {
        this._super();
        var self = this;

        Ember.$("#department_roles").sortable({
            update: function () {
                var indices = {};
                Ember.$(this).children().each((index, item) => {
                    indices[Ember.$(item).data('id')] = index + 1;
                });

                self.updateSortedOrder(indices);
            },
        });

        Ember.$('#department_roles').disableSelection();
    },
    actions: {
        removeItem: function (itemId) {

            var self = this,
                deleteItem,
                list = this.get('list');

            this.$("#department_roles").sortable("destroy");

            list.arrayContentWillChange();

            list.forEach(function (item) {

                if (item.id === itemId) {
                    deleteItem = item;
                }
            });

            list.removeObject(deleteItem);

            list.arrayContentDidChange();

            Ember.$("#department_roles").sortable({
                update: function () {
                    var indices2 = {};
                    Ember.$(this).children().each((index, item) => {
                        indices2[Ember.$(item).data('id')] = index + 1;
                    });
                    self.updateSortedOrder(indices2);

                },
            });

            console.log(self.get('sortedList'));
        }
    }
});
  • Is this printing `console.log(self.get('sortedList'));` sorted list after removing?. – Ember Freak Sep 23 '16 at 14:54
  • @kumkanillam yes the printed list is fine but the DOM is wrong. – user1432760 Sep 23 '16 at 15:02
  • just remove this line `list: Ember.ArrayProxy.create({content: Ember.A()})` and include the below line in `init()` - this.set('list',[]); I meant can you try this with normal array instead of ArrayProxy..Is there any specific reason for ArrayProxy ?. – Ember Freak Sep 23 '16 at 15:10
  • @kumkanillam I try with normal [], with Ember.A and always it is the same result. I remove the line and and behaves the same. Here is how DOM looks like after sort [link](http://imgur.com/Z9DEEtC). I was add extra {{item.id}} before
  • tag to see how each works and the each order is not the same as
  • tags. After you delete item 3 the item 2 is also removed from DOM because it is after 3.
  • – user1432760 Sep 23 '16 at 15:24
  • May be you can try addon https://github.com/jgwhite/ember-sortable or this [stack overflow question](http://stackoverflow.com/questions/10298057/best-way-to-update-collectionview-content-after-jquery-ui-sortable) – Ember Freak Sep 27 '16 at 10:13
  • Problem is you should shift object from current index to moved index positions, not just index property alone. – Ember Freak Sep 27 '16 at 10:57
  • @kumkanillam I try that addon but is the same result, finaly I decide to use cancel option with index on items. After I set index on item I call cancel and sort items by index. Problem is what DOM is in conflict with ember each helper so after sort or delete item list is incorrect but cancel fix that problem. – user1432760 Sep 28 '16 at 23:02
  • @user1432760 could you please update your own question or give answer to your question, so it will help others. Thank youj – Aamir Mahmood Oct 19 '16 at 10:10