1
0
Fork 0

Fixed sorting/adding bug

This commit is contained in:
Ava Gaiety Wroten 2019-12-22 12:35:58 -06:00
parent 793f8ba79a
commit a57ca025f5
3 changed files with 15 additions and 4 deletions

View file

@ -1,7 +1,7 @@
<div class="rounded-md shadow-md"> <div class="rounded-md shadow-md">
<SortableGroup <SortableGroup
@model={{this.items}} @model={{this.sortedItems}}
@onChange={{action this.reorderItems}} @onChange={{action this.reorderMeals}}
as |group|> as |group|>
{{#each group.model as |meal|}} {{#each group.model as |meal|}}
<group.item <group.item

View file

@ -13,13 +13,23 @@ export default class RecipeListComponent extends Component {
this.assignExistingItems(); this.assignExistingItems();
} }
get sortedItems() {
console.log('sorted items called');
return this.items.sortBy('listOrder');
}
async assignExistingItems() { async assignExistingItems() {
let items = await this.store.peekAll('meal'); let items = await this.store.peekAll('meal');
this.items = items; this.items = items;
} }
@action @action
reorderItems(reorderedItems) { async reorderMeals(reorderedMeals) {
this.items = reorderedItems; let orderedIds = reorderedMeals.map(meal => meal.id);
for (let index = 0; index < orderedIds.length; index++) {
let meal = await this.store.peekRecord('meal', orderedIds[index]);
meal.listOrder = index;
}
} }
} }

View file

@ -11,4 +11,5 @@ export default class MealModel extends Model {
@attr tags; @attr tags;
@attr youtube; @attr youtube;
@attr source; @attr source;
@attr('number', { defaultValue: Infinity }) listOrder;
} }