Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Created August 27, 2015 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexhawkins/514c15735bba99d0850e to your computer and use it in GitHub Desktop.
Save alexhawkins/514c15735bba99d0850e to your computer and use it in GitHub Desktop.
cool lodash code
/* checks to see if item id arrangement in packages has changed */
let compareItemIds = (oldIds, newIds) => {
return _.difference(newIds, oldIds).concat(_.difference(oldIds, newIds));
};
/* gets the item of ids of current packages and orignal packages
and compares them for changes */
let haveItemsChanged = () => {
let curIds = [];
let oldIds = [];
let currentPackages = $scope.packages;
let orginalPackages = $scope.orginalPackages;
return _.filter(orginalPackages, (pkg, index) => {
_.each(pkg.package_items, (item) => {
oldIds.push(_.pick(item, 'id').id);
});
_.each(currentPackages[index].package_items, (item) => {
curIds.push(_.pick(item, 'id').id);
});
return [].concat(compareItemIds(oldIds, curIds)).length > 0;
}).length > 0;
};
/* updates items on drop if item different than
initial drag item, resets selected to true */
$scope.onDrop = () => {
$scope.itemsSwapped = haveItemsChanged();
if ($scope.itemsSwapped) {
$scope.reset(); // make sure all packages are selected on swap
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment