Peter Boomsma
Peter Boomsma

Reputation: 9808

Switch between 2 ng-shows

I have two elements with a ng-show in them,

%a.follow{"ng-click" => "followUser(user)", "ng-show" => "!isFollowed(user.id)"} follow
%a.unfollow{"ng-click" => "unfollowUser(user)", "ng-show" => "isFollowed(user.id)"} unfollow

It depends on the user.id which ng-show is being rendered in the template. So only one of the two ng-shows is displayed.

So for example a user wants to start following another user. Then the follow link is displayed.

%a.follow{"ng-click" => "followUser(user)", "ng-show" => "!isFollowed(user.id)"} follow

When a user clicks on it, I would like to hide the clicked ng-show, and show the unfollow ng-show so that the user can unfollow the just followed user.

The follow and unfollow user function,

$scope.followUser = function (user) {
  followUser.create({
    followed_id: user.id
  }).then(init);
  Notification.success(user.name + ' is toegevoegd als vriend.');
}

$scope.unfollowUser = function(user){
  unfollowUser.unfollowUser(user).then(function(){
  },function(){
  }).then(init);
  Notification.success(user.name + ' is verwijderd als vriend.');
}

And the isFollowed function,

usersService.loadUsers().then(function(response) {
  $scope.users = response.data;
  console.log ($scope.users)

  angular.forEach(response, function(user){
    $scope.user = user

    $scope.isFollowed = function(userId) {
      var following = $scope.current_user.following;
      for (var i=0; i<following.length; i++) {
        if (following[i].id == userId) {
          return true;
        }
      }
      return false;
    }
  })
})

I've tried building this,

<a ng-click="follow=false ;unfollow=true", ng-show="follow">Follow!</a>
<a ng-click="follow=true; unfollow=false", ng-show="unfollow">Unfollow!</a>

This does switch between the two ng-shows, but when I try to get the isFollowed(user.id), !isFollowed(user.id) in them the code crashes.

Upvotes: 1

Views: 121

Answers (2)

Peter Boomsma
Peter Boomsma

Reputation: 9808

Alrhgout Satpal did point me to the right direction and helped me with some code. His answer isn't complete. So I've decided that add the code I'm using for this function (made with the help of Satpal!).

I've created a followUnfollowUser function. But instead of having two .then(init) I have one init() at the end of the function. Having the two inits gave me some looping trouble.

$scope.followUnfollowUser = function(user) {
    //If followed - unfollow
    if (user.isFollowed) {

        unfollowUser.unfollowUser(user).then(function() {
            user.isFollowed=!user.isFollowed
        }, function() {
        })

        Notification.success(user.name + ' is verwijderd als vriend.');

    } else {

        followUser.create({
            followed_id: user.id
        }).then(function() {
            user.isFollowed=!user.isFollowed
        }, function() {
        })

        Notification.success(user.name + ' is toegevoegd als vriend.');
    }
  init();
}

Then the init function,

var init = function () {

  loadCurrent_user.loadCurrent_user().then(function(response) {
    $scope.current_user = response.data;
  });

  usersService.loadUsers().then(function(response) {
    $scope.users = response.data;
    //Iterate and create isFollowed property
    angular.forEach($scope.users, function(user) {
      user.isFollowed = isFollowed(user.id);
    })
  })

  var isFollowed = function(userId) {

    var following = $scope.current_user.following;
    for (var i = 0; i < following.length; i++) {
      if (following[i].id == userId) {
          return true;
      }
    }
    return false;
  }

}

First I load the current user so that the $scope.current_user gets updated when a user is being followed/unfollowed. And then I iterate through each user and create the isFollowed value using the isFollowed function.

And in my template I have,

%a{"ng-click" => "followUnfollowUser(user)"}
  -# {{ user.isFollowed }}
  {{ user.isFollowed ? "Unfollow user" : "Follow user"}}

Upvotes: 0

Satpal
Satpal

Reputation: 133403

You should create single function to follow/unfollow, Here in the code snippet I have introduced a new property i.e. isFollowed to object user whose value is set using the isFollowed function.

Additionally, Don't overuse isFollowed(user.id) method, it will be huge performance hit.

HTML

<a ng-click="followUnfollowUser(user)"> {{ user.isFollowed : "Unfollow!" : "Follow!"}}  </a>

Script

$scope.followUnfollowUser = function(user) {
    //If followed - unfollow
    if (user.isFollowed) {
        unfollowUser.unfollowUser(user).then(function() {
            user.isFollowed=!user.isFollowed
        }, function() {
        }).then(init);

        Notification.success(user.name + ' is verwijderd als vriend.');
    } else {

        followUser.create({
            followed_id: user.id
        }).then(function() {
            user.isFollowed=!user.isFollowed
        }, function() {
        }).then(init);

        Notification.success(user.name + ' is toegevoegd als vriend.');
    }
}

//Define method to check wheather current user is beign followed
var isFollowed = function(userId) {
    var following = $scope.current_user.following;
    for (var i = 0; i < following.length; i++) {
        if (following[i].id == userId) {
            return true;
        }
    }
    return false;
}

//Fetch Users
usersService.loadUsers().then(function(response) {
    $scope.users = response.data;
    //Iterate and create isFollowed property
    angular.forEach($scope.users, function(user) {
        user.isFollowed = isFollowed(user.id);
    })
})

Note: I'm not familiar with following syntax thus used standard HTML.

%a.follow{"ng-click" => "followUser(user)", "ng-show" => "!isFollowed(user.id)"} follow

Upvotes: 1

Related Questions