-
-
Save zdennis/0d32b41105fad5e5e77ff9e2666b5477 to your computer and use it in GitHub Desktop.
Swap nodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
filter: 'all', | |
init: function () { | |
this._super(); | |
Ember.run.schedule("afterRender",this,function() { | |
this.updateSortable(); | |
}) | |
}, | |
updateSortable: function(){ | |
let $el = $(".ui-sortable"); | |
let updateParts = (evt) => { | |
this.send("updateParts", evt) | |
}; | |
let startIndex; | |
let nextSibling; | |
$el.sortable({ | |
start(evt, ui) { | |
startIndex = ui.item.index(); | |
// on start a placeholder is inserted after | |
// stash our original nextSibling | |
// since cancel doesn't undo correctly | |
nextSibling = ui.placeholder[0].nextSibling; | |
}, | |
update(evt, ui) { | |
let item = ui.item; | |
let itemEl = item[0]; | |
let endIndex = item.index(); | |
updateParts({ startIndex, endIndex }); | |
$el.sortable('cancel'); | |
// the above doesn't undo mutation correctly | |
itemEl.parentNode.insertBefore(itemEl, nextSibling); | |
} | |
}); | |
}, | |
_parts: [ | |
{ name: "Green", isArchived: false }, | |
{ name: "Blue", isArchived: false }, | |
{ name: "Yellow", isArchived: true } | |
], | |
parts: Ember.computed('_parts.@each.isArchived', 'filter', function () { | |
if (this.get('filter') === 'all') { | |
return this.get('_parts'); | |
} else { | |
return this.get('_parts').filter((part) => !part.isArchived); | |
} | |
}), | |
actions: { | |
updateParts: function(evt) { | |
let { startIndex, endIndex } = evt; | |
let parts = this.get('parts'); | |
let part = parts[startIndex]; | |
parts.removeAt(startIndex); | |
parts.insertAt(endIndex, part); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.12.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.12.0", | |
"ember-template-compiler": "2.12.0", | |
"ember-testing": "2.12.0" | |
}, | |
"addons": { | |
"ember-data": "2.12.1", | |
"ember-ui-sortable": "0.3.3" | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment