0

Using Angular Material in the angular-material-fullstack Yeoman generator, I am incorporating a toast:

var last = {
  bottom: false,
  top: true,
  left: false,
  right: true
};

$scope.toastPosition = angular.extend({},last);

function sanitizePosition() {
  var current = $scope.toastPosition;
  if ( current.bottom && last.top ) current.top = false;
  if ( current.top && last.bottom ) current.bottom = false;
  if ( current.right && last.left ) current.left = false;
  if ( current.left && last.right ) current.right = false;
  last = angular.extend({},current);
}

$scope.getToastPosition = function() {
  sanitizePosition();
  return Object.keys($scope.toastPosition)
    .filter(function(pos) { return $scope.toastPosition[pos]; })
    .join(' ');
};

var showSimpleToast = function() {
  $mdToast.show(
    $mdToast.simple()
      .content('Please enter a valid stock name.')
      .position($scope.getToastPosition())
      .hideDelay(3000)
  );
};

What could I do to 'get this code out of the way'? Is it worth implementing into a service? I simply don't want it to take up so much room in my controller.

alanbuchanan
  • 3,993
  • 7
  • 41
  • 64
  • move your toast logic to a factory. – levi Nov 04 '15 at 15:19
  • well either the toast is in a directive you wrap for your purposes or you have the logic in a factory and call from a directive or controller to load toast... depends on how you're gonna use it and where... I would use it as a directive since it manipulates DOM and have the add remove function called from a factory – Jony-Y Nov 04 '15 at 15:41
  • 1
    a very opinionated question. lots of possible answers. – Reactgular Nov 04 '15 at 15:41

1 Answers1

0

Pardon me if I'm misunderstanding your question, but are you looking for this?

$mdToast.show(
    $mdToast.simple()
    .content('Please enter a valid stock name.')
    .position('top right')
    .hideDelay(3000);
);

The extra code they have is just so that you can't check two opposing checkboxes or have a direction missing, e.g. top bottom loses bottom and gains a default side direction.

Bryan K
  • 437
  • 4
  • 15